How to change the post type slug without losing data inside post type.

  • Step 1 – Go to your activated theme
  • Step 2 – Open your Function.php inside the theme
  • Step 3 – Copy Code and change slug of the post with your post type slug
  • Step 4 – Past copy code to function.php

Step 1 – Go to your activated theme

Activated theme you are find inside the Dashboard > Appearance > Themes

Step 2 – Open your Function.php inside the theme


you can find your theme function.php file on path:your-project-folder/wp-content/themes/twentynineteen/functions.php
Note: twenty nineteen is the theme name which you activate for your project

Step 3 – Copy Code and change slug of the post with your post type slug

Just Copy code and change the slug with your post type slug and replace stories with your new post type slug.
Note: You can change the ‘portfolio’ with your current post type slug and replace stories with your new post type slug.

add_filter( 'register_post_type_args', 'wptrinity_post_type_args', 10, 2 );
function wptrinity_post_type_args( $args, $post_type ) {

    if ( 'portfolio' === $post_type ) {
        $args['rewrite']['slug'] = 'stories';
    }

    return $args;
}

Step 4 – Past copy code to function.php

After changes apply inside the code in “Step 4″ you need to add changed code into your function.php file.
Now Go to the wp-admin you find the slug of your post type changes.

Leave a Comment

Your email address will not be published. Required fields are marked *