13

I have a custom post type of portfolio (slug portfolio) in my theme and all is working well except one thing. When people create a page with a slug of portfolio, eg: example.com/portfolio, the theme thinks I'm wanting to use the custom post type archive page, not my page created in the editor.

Can I somehow "reserve" the slug portfolio when pages are saved to be sure it never gets used, maybe renamed to portfolio-page?

My project uses the methods outlined in this tutorial, here's the conflict I'm referring to:

"Beyond having to patch the code there is one other catch here in that you can’t call your Page slug the same thing as your custom post type slug. Think of your custom post type slug as a reserved keyword; however, you can make the title of your Page the same name as your custom post type slug just as long as your Page slug is something different." - http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/

Thanks for any advice!

2
  • One option I see would be to remove this from the register_post_type() init: 'rewrite' => array( 'slug' => 'portfolio', 'with_front' => true ), and just set 'rewrite' => false and just live with the ugly "single" portfolio items.
    – hhins
    Commented Oct 19, 2011 at 15:01
  • Vote up for your question as even I want to know the answer! I have worked with custom posts for a couple of times, but I never noticed this, and now after reading your question, I tried it. You're right, WordPress does get confused here! Commented Oct 19, 2011 at 15:03

3 Answers 3

14

The following 2 filters allow you to hook into when WordPress checks the slug and are found in the function wp_unique_post_slug() in the wp-includes/post.php file.

There are 2 filters, one for hierarchical posts and one for non-hierarchical. The hierarchical filter provides the ID for the post parent so if the $post_parent is 0, you know this is a "base" post.

The filters also pass the $post_type if you wanted to add specific post types to your logic.

By returning "true", you're telling WordPress that this slug is bad so WordPress adds on a suffix, just like it would do if you were trying to us a post name/slug that is already taken.

add_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'portfolio_is_bad_hierarchical_slug', 10, 4 );
function portfolio_is_bad_hierarchical_slug( $is_bad_hierarchical_slug, $slug, $post_type, $post_parent ) {
    if ( !$post_parent && $slug == 'portfolio' )
        return true;
    return $is_bad_hierarchical_slug;
}

add_filter( 'wp_unique_post_slug_is_bad_flat_slug', 'portfolio_is_bad_flat_slug', 10, 3 );
function portfolio_is_bad_flat_slug( $is_bad_flat_slug, $slug, $post_type ) {
    if ( $slug == 'portfolio' )
        return true;
    return $is_bad_flat_slug;
}
1
  • For some reason I am still facing the same problem
    – Tan-007
    Commented Nov 14, 2019 at 16:53
1

Create a page with a slug of 'portfolio' and make it 'Private'. It will be hidden from public when reserving the slug from futher usage.

1

Came across exactly this problem when investigating a pagination problem. We have a News homepage (slug "news") and pagination was failing ... attempts to get to /news/page/2/ always resulted in '404 - Not Found'.

The problem turned out to be that our News custom type had also been given the slug "news". By changing the latter (for example to "news_archive") the problem was fixed. This left the original news URL references unchanged. Changing the News homepage slug also worked but then all URLS would incorporate the changed slug.

Your Answer

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

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