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!
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!
You must be logged in to post a comment.
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,I think you can use the answer to this question by replacing
page_rewrite_rules
filter withpost_rewrite_rules
, making post rewrite rules to have the top most priority.