Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
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 Oct 19 '11 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! –  Rutwick Gangurde Oct 19 '11 at 15:03
add comment

3 Answers

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;
}
share|improve this answer
    
Hi Rachel, could you please kindly look at my post wordpress.stackexchange.com/questions/111334/… I feel like you are an expert –  Alex Aug 23 '13 at 14:47
    
I answered your question. :) –  Rachel Carden Aug 26 '13 at 19:11
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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