10

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.

26

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;
}
2
  • 1
    Amazing. This worked! – Darren Bachan Nov 25 '16 at 6:42
  • 2
    This worked for me after applying the additional step mentioned by @Felipe Romero: "refresh" your Permalinks by going to Settings > Permalinks and hitting Save changes. – Ralf Feb 6 '20 at 16:06
5

@dave-romsey answer didn't work for me, PHP kept telling me Warning: Cannot use a scalar value as an array in /path/to/functions.php So I went the array_merge way.

Complete function you need to add to your child's theme functions.php file:

function update_portfolios_slug( $args, $post_type ) {

    if ( 'portfolios' === $post_type ) {

        //$args['rewrite']['slug'] = 'presidentes';

        $my_args = array(
            'rewrite' => array( 'slug' => 'presidentes', 'with_front' => false )
        );

        return array_merge( $args, $my_args );
    }

    return $args;
}

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

After uploading your code don't forget to "refresh" your Permalinks by going to Settings > Permalinks and hitting Save changes.

Cheers.

2
  • This got me to the finish line but one thing to note is that array_merge doesn't seem to merge sub arrays. When I removed the slug (as it was custom set by the plugin) it removed the base but also reset to its default cpt slug name. It seems it will merge the rewrite' into the rest of the $args but just replace all of rewrite, not merge that as well. Conclusion - set all of your rewrite values. – rtpHarry Nov 29 '19 at 12:56
  • @rtpHarry this is really useful to know. I'm sure it'll help others. Thank you for your contribution! – Felipe Romero Dec 5 '19 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.