11

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 Answers 2

31

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;
}
5
  • 1
    Amazing. This worked! Nov 25, 2016 at 6:42
  • 3
    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, 2020 at 16:06
  • @DaveRomsey, Can we use the same name for page slug and custom post type slug? Feb 11, 2022 at 15:27
  • @NarenVerma Technically you can, but there will be permalink conflicts. I'd use unique slugs. Feb 12, 2022 at 17:59
  • @DaveRomsey, Actually I have created a custom post type called "blog". Now I have also created a blog page and Yes it conflicts with the URL. What I am doing is, I have to show all the blog posts on this url example.com/blog and I also need a URL for a single-page blog called example.com/blog/post-name Feb 14, 2022 at 5:41
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, 2019 at 12:56
  • @rtpHarry this is really useful to know. I'm sure it'll help others. Thank you for your contribution! Dec 5, 2019 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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