• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Change Custom Post Type Slug Wordpress?

darkid

Active member
Mar 1, 2020
203
41
28
How to change the default custom post type slug of the WordPress theme?
 
An try this
Yes, I've tried but the post goes blank? Can you help me, I will send the theme via PM.
I just want to change domain.com/tour/hongkong to domain.com/rute/hongkong.
 
Yes, I've tried but the post goes blank? Can you help me, I will send the theme via PM.
I just want to change domain.com/tour/hongkong to domain.com/rute/hongkong.
I'll try as I can
 
  • Like
Reactions: darkid
After changing the CPT slug with one of the methods described in the link above, you should resave your permalinks. Under Settings > Permalinks.
I have tried using Visual Studio Code, changing "tour" to "rute" using the find and replace menu and the slug was successfully replaced, but the single post type rute is blank.

Can you help me?
 
How to change the default custom post type slug of the WordPress theme?

The following snippet will do it for you. Remember to check the post type/taxonomy actual name hovering the menu item of it on WordPress admin dashboard (E.g: yoururl.com/wp-admin/edit.php?post_type=XXXXXX..., yoururl.com/wp-admin/edit.php?taxonomy=XXXXXX...).

PHP:
// PHP 8.x syntax: function changePostTypesSlug( array $args, string $postType ): array {
function changePostTypesSlug( $args, $postType ) {
    switch($post_type) {
        case 'YOUR_CUSTOM_POST_TYPE':

            // Changing the slug
            $args['rewrite']['slug'] = 'YOUR_NEW_SLUG';

            break;
    }

    return $args;
}
add_filter( 'register_post_type_args', 'changePostTypesSlug', 10, 2 );

// PHP 8.x syntax: function changeTaxonomiesSlug( array $args, string $taxonomy ): array {
function changeTaxonomiesSlug( $args, $taxonomy ) {
    switch($taxonomy) {
        case 'YOUR_CURRENT_TAXONOMY':

            // Here you can also change the labels easily
            $args['labels'] = [
                'name'              => 'Categories',
                'singular_name'     => 'Category',
                'search_items'      => 'Search Category',
                'all_items'         => 'All Categories',
                'parent_item'       => 'Category parent',
                'parent_item_colon' => 'Category parent:',
                'edit_item'         => 'Edit Category',
                'update_item'       => 'Update Category',
                'add_new_item'      => 'New Category',
                'new_item_name'     => 'New Category',
                'menu_name'         => 'Categories'
            ];
       
            // Changing the slug
            $args['rewrite']['slug'] = 'YOUR_NEW_SLUG';
       
            break;
    }
 
   return $args;
}
add_filter( 'register_taxonomy_args', 'changeTaxonomiesSlug', 10, 2 );

After doing this, just update your permalinks and it will work perfectly. Also, if you're not using custom code snippets actually, I suggest the WP Code plugin to manage it.

This snippet was tested and used on some projects so, 100% working.
 
Last edited:
  • Like
Reactions: kyokusnagi
Yes, I've tried but the post goes blank? Can you help me, I will send the theme via PM.
I just want to change domain.com/tour/hongkong to domain.com/rute/hongkong.

The code solution provided by @kalil is also viable and does the thing.

If you're looking for a more user-friendly, no-code solution:
  1. Install and activate the Custom Post Type UI free plugin.

  2. Go to CPT UI - Add/Edit Post Types menu >Edit Post Types tab (left img ⬇️)

  3. Select post type you want to change from the dropdown on the new tab

  4. Edit what you need and save

    ⚠️ To migrate the current posts and their content be sure to check the box before Migrate posts to newly renamed post type?
    ⚠️ MAKE SURE TO READ AND UNDERSTAND EVERYTHING BELOW THE SLUG INPUT FIELD
    ⚠️
    It's spelled "route" , not "rute" in English

  5. Go to Settings > Permalinks menu (right img ⬇️) and click "Save Changes" to trigger a permalink refresh.

    Personally, I would create new post type, export all existing posts from the current post type and re-import them to my the fresh one. This is a cleaner solution and it's completely independent of any current or future theme code or settings that work with it's own registered CPT.

    ⚠️ Highly recommended

    USE A CHILD THEME FOR ALL CUSTOM WORK THAT MODIFIES THEME CONTENT/STRUCTURE.
    This is useful to prevent potential conflicts and overwrites caused by future theme updates.

    For example: If you use VS Code to search and replace in the theme-folder, all your changes will be overwritten when you update the theme.



    Good luck !

    1705457899478.png Permalink screen =
 
Last edited:
  • Like
Reactions: kalil
The code solution provided by @kalil is also viable and does the thing.

If you're looking for a more user-friendly, no-code solution:
  1. Install and activate the Custom Post Type UI free plugin.

  2. Go to CPT UI - Add/Edit Post Types menu >Edit Post Types tab (left img ⬇️)

  3. Select post type you want to change from the dropdown on the new tab

  4. Edit what you need and save

    ⚠️ To migrate the current posts and their content be sure to check the box before Migrate posts to newly renamed post type?
    ⚠️ MAKE SURE TO READ AND UNDERSTAND EVERYTHING BELOW THE SLUG INPUT FIELD
    ⚠️
    It's spelled "route" , not "rute" in English

  5. Go to Settings > Permalinks menu (right img ⬇️) and click "Save Changes" to trigger a permalink refresh.

    Personally, I would create new post type, export all existing posts from the current post type and re-import them to my the fresh one. This is a cleaner solution and it's completely independent of any current or future theme code or settings that work with it's own registered CPT.

    ⚠️ Highly recommended

    USE A CHILD THEME FOR ALL CUSTOM WORK THAT MODIFIES THEME CONTENT/STRUCTURE.
    This is useful to prevent potential conflicts and overwrites caused by future theme updates.

    For example: If you use VS Code to search and replace in the theme-folder, all your changes will be overwritten when you update the theme.



    Good luck !

    1705457899478.png 1705458058921.png =
Thanks for the addict and pretty sure, that's one of some more user friendly alternatives! Just shared the code because it's on my "favorites list" for needs on my projects :ROFLMAO:. Sorry not mention something more user-friendly also but, @unusual_research make a good one here for us!
 
  • Like
Reactions: unusual_research
Thanks for the addict and pretty sure, that's one of some more user friendly alternatives! Just shared the code because it's on my "favorites list" for needs on my projects :ROFLMAO:. Sorry not mention something more user-friendly also but, @unusual_research make a good one here for us!
Well, I'd still create a completely new custom post type and work it from there. But that depends on the theme OP is using.

Probably the theme has some pre-designed modules hardcoded for that particular custom post type it registers or something.

If that's the case, the theme is not worth it so I don't see any reason not to go for a new one :)
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock