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 wordpress site where the category base is stripped from the url.

Now, when a category has a certain slug that's similar to a post slug, the category is shown.

I would like to show the post instead. Is this possible?

Thanks!

share|improve this question
    
Can you post the slugs that are causing the problem and explain the mechanism you used to strip the category from the URL? –  s_ha_dum Dec 10 '12 at 16:31
    
example url: example.com/wordpress/cars where "cars" is both slug for a post and for a category. The mechanism I've used for stripping the category base is the same that is used in the WP No Category Base plugin. –  bertkat Dec 10 '12 at 16:46

2 Answers 2

I think you can use the answer to this question by replacing page_rewrite_rules filter with post_rewrite_rules, making post rewrite rules to have the top most priority.

share|improve this answer
    
At first it seemed to work. Post are shown when they should. But now all categories disappeared, I can't get them to show up. Thanks in advance! –  bertkat Dec 11 '12 at 13:00
    
@bertkat You mean, you have 404/not found error on categories? Did you try flushing the rewrite rules? –  Parham Dec 11 '12 at 13:08
    
Yes Parham, I have 404 on all categories. Also tried flushing rewrite rules and also tried without stripping category base. –  bertkat Dec 11 '12 at 13:42

There isn't a clean way to do what you're asking. What you want is for WordPress to check to see if, for a given URL, there is a post with that slug, and if not, try to find a category with that slug. With the exception of pages, WordPress doesn't check to see if an opject (post, term) exists before "committing" to a matching rewrite rule. Therefore, when you have conflicting rewrite rules, the second one will never be read.

The best answer is to find a different rewrite structure for your posts or categories. Prefixes are ideal, like /blog/%postname%/, /articles/%postname%/ for posts, or /category/%category%/, /topic/%category%/ for categories.

That said, it's not impossible to do what you're asking, it's just not optimal. What you need to do is intercept the request and check to see if the post exists. If not, alter the query vars. Note that this adds to every post's page load, so weigh the costs against the benefits. Also remember that if a post and category share the same name, there's no accessing the category. Without further ado,

function wpse_75604_check_rewrite_conflicts( $qv ) {
    if ( isset( $qv['name'] ) ) {
        if ( ! get_page_by_path( $qv['name'], OBJECT, 'post' ) ) {
            $qv['category_name'] = $qv['name'];
            unset( $qv['name'] );
        }
    }
    return $qv;
}
add_filter( 'request', 'wpse_75604_check_rewrite_conflicts' );
share|improve this answer

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.