Home » How To Change Custom Post Type Slug In WordPress

How To Change Custom Post Type Slug In WordPress

Last updated on December 27, 2020 by

Sometimes, WordPress users want to change custom post type slug instead of the default one. In such a case, we will show you the right and good way to do it with very simple step. So are you ready to do it? Let’s just jump into it.

Table of Contents
1. Considerations Before Changing The Custom Post Type Slug
2. An Ideal Place to Add Code in WordPress
3. Change Slug of a Plugin’s Custom Post Type (CPT)
4. If You Have Already Created a Custom Post Type (CPT) Using register_post_type Function
5. If You Have Already Created a Custom Post Type Using CPT UI Plugin

Considerations Before Changing The Custom Post Type (CPT) Slug

There are some naming conventions for the slug in WordPress. So consider the following points before writing your new slug.

  1. The new slug should be unique otherwise, it won’t work and led you to the 404 page
  2. For good SEO use only lowercase letters
  3. White spaces are not allowed
  4. Use dashes (-) to separate slug’s words like my-custom-post-type-slug

An Ideal Place to Add Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please also note that we has tested all codes with the GeneratePress child theme & Storefront child theme.

Steps to Change The Custom Post Type (CPT) Slug

There might be numerous ways to do this. You can do this with or without a plugin. It’s a very simple task, so we don’t recommend using the plugin but if you want to do with plugin then you can.

01 Change Slug of a Plugin’s Custom Post Type (CPT)

To do this, we can use the register_post_type_args filter hook. So by using this hook we can rewrite our custom post type’s slug.

Let’s change the slug by taking a very simple example. Let’s assume, you have a "My Customers" custom post type and its default slug is "my-customer" so now you want to change the slug to "customers". So you can do that by following way.

/*@ Rewriting slug of CPT */

if( !function_exists('sc_change_custom_post_type_slug') ):

	add_filter( 'register_post_type_args', 'sc_change_custom_post_type_slug', 10, 2 );

	function sc_change_custom_post_type_slug( $args, $post_type ) {

	    if ( 'my-customer' === $post_type ) {
	        $args['rewrite']['slug'] = 'customers';
	    }

	    return $args;
	}

endif;

On the above example, we have added a function with a filter hook. Then inside a function, we added if condition to check whether the post type is 'my-customer' or not if it is 'my-customer' then we are changing the slug to 'customers'. That’s it.

Notes: In order to make the above example work, you need to change your current custom post type slug on highlighted line no. 9 and your new slug on line no. 10

Go to Settings -> Permalinks and re-save the permalinks by clicking the “Save Changes” button after modifying the code as this will flush the rewrite rules which are needed to make the changes.

02 If You Have Already Created a Custom Post Type (CPT) Using register_post_type Function

If you have already created a custom post type by using a register_post_type function then, it will be very easy to change the slug. You just need to add one more argument 'rewrite' => array('slug' => 'customers') into the register_post_type.

Let’s see simple example so that you can understand better.

  1. Let’s assume, you have already created a custom post type by the following code.
function my_customer_post_type_setup() {
    $args = array(
        'public'    => true,
        'label'     => __( 'My Customers', 'textdomain' ),
        'menu_icon' => 'dashicons-book',
    );
    register_post_type( 'my-customers', $args );
}
add_action( 'init', 'my_customer_post_type_setup' );
  1. Now, add the rewrite argument as below into your register_post_type function.
function my_customer_post_type_setup() {
    $args = array(
        'public'    => true,
        'label'     => __( 'My Customers', 'textdomain' ),
        'menu_icon' => 'dashicons-book',
        'rewrite'   => array( 'slug' => 'customers' ),
    );
    register_post_type( 'my-customers', $args );
}
add_action( 'init', 'my_customer_post_type_setup' );

Go to Settings -> Permalinks and re-save the permalinks by clicking the “Save Changes” button after modifying the code as this will flush the rewrite rules which are needed to make the changes.

03 If You Have Already Created a Custom Post Type Using CPT UI Plugin

Lots of WordPress users are using the Custom Post Type UI plugin for adding custom post types. So sometimes, they might want to change the slug of CPT instead of changing the default “Post Type Slug”.

If user directly change the slug from Basic Settings -> “Post Type Slug” textbox then it will erase all the data. So let’s change the slug in a right way.

  1. Go to CPT UI -> Add/Edit Post Types submenu
  2. Then click on Edit Post Types tab
  3. Now, select your post type from the dropdown
change custom post type slug in CPT UI plugin
  1. After that scroll down and down to the "Custom Rewrite Slug" input box
  2. Now, enter your new slug into that textbox like “customers”
change custom post type slug in CPT UI plugin
  1. Last but not least, scroll down to the end of the page and click on "Save Post Type" button.

Additionally read our guide, How to Remove the Category From URL in WordPress

That’s it for now. We hope this article helped you to change the custom post type slug in WordPress.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

3 thoughts on “How To Change Custom Post Type Slug In WordPress”

Leave a Comment