Change Custom Post Type slug

I’m working within a child theme so I don’t want to edit the file that is registering a Portfolio CPT to my site. I used a plugin to change the name from Portfolio to Stories, but the plugin doesn’t give an option for the slug.

I tried using the following function:

function change_slug_of_post_type_portfolio() {
    register_post_type('portfolio', array('rewrite' => array ('slug' => 'stories',)));
}
add_action('init', 'change_slug_of_post_type_portfolio', 20);

But it removes Portfolio entirely from the WordPress admin sidebar.

2

The register_post_type_args filter can be used to modify post type arguments:

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

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

    return $args;
}

Leave a Comment