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 website with 20+ categories. To make it easier to navigate I want to decrease the categories, but at the same time keep the original categories. In other words I want to be able to query multiple categories with a category group or a virtual slug.

Example categories:

/category/apples/
/category/pears/
/category/bananas/

I want to be able to query all three of them with a unique url slug:

/category/fruit/

But like I said, it should still be possible to go to i.e. /category/bananas/

I know it's possible to query multiple categories the way I want to using /category/apples,bananas,pears/ but it's not a elegant solution.

Is it possible to do this virtual grouping with a plugin or even using a rewrite?

Tips and recommendations are greatly appreciated!

share|improve this question

migrated from stackoverflow.com Nov 5 '12 at 17:06

This question came from our site for professional and enthusiast programmers.

    
I do not want to use subcategories for this. Any other suggestions are welcome! –  Chris Nov 4 '12 at 12:58
    
Bump, I am sure there is someone out there who knows what I want to do without using parent categories. –  Chris Nov 9 '12 at 18:15

2 Answers 2

Wouldn't WordPress's built-in sub-categories functionality solve this problem?

 + fruit
  + bananas
  + apples
  + pears

you would get all of fruit through

/categories/fruit

but also specific categories through

/categories/fruit/bananas

to add a sub-category, just choose fruit as the parent category for each child.

share|improve this answer
    
I do not want to use any subcategories. This defeats the purpose of keeping the original categories and permalinks. I also like to keep it this way because of SEO reasons. –  Chris Nov 3 '12 at 17:26
    
Hmm. Would it then not be simpler to try and hide bananas, apples, and pears from the navigation, but keep them as categories? That might be less work than finding a plugin that rewrites the handling of categories fundamentally –  Pekka 웃 Nov 3 '12 at 17:28
    
What I want is to be able to place a link to these virtual categories in my navigation and display posts attached to these categories. –  Chris Nov 3 '12 at 17:30
    
@Chris as said, it would probably be easier to have fruit as a separate (but real) category in your category tree, alongside bananas, apples, and pears, but hiding those in the menu. So an apple entry is both in the /apple and the /fruit category, but only the /fruit category is visible in the menu. (But /apple still works for SEO). Is that a workable option? I'm not aware of a plugin that does exactly what you want (although there may be one) –  Pekka 웃 Nov 3 '12 at 17:33
    
I understand what you are saying, but that would require to create new parent categories for the navigation and then move the 20+ categories into those as child categories. I want to still be able to use all my inlinks to my pages and I believe regrouping the categories this way will create alot of 404 pages, true? –  Chris Nov 3 '12 at 17:34

You could override your query with pre_get_posts in functions.php:

function add_all_fruits_to_category($query) {
    $catnames = $query->get('category_name');
    if ($catnames == 'fruits') {
        $query->set('category_name', $catnames . ',bananas,apples,pears');
    }
}
add_action('pre_get_posts', 'add_all_fruits_to_category');
share|improve this answer
    
Thanks Virgil! This is what I wanted from the getko! It works very well, except that I get a couple of warning messages: Warning: rtrim() expects parameter 1 to be string, object given in wp-includes/formatting.php on line 1381 Catchable fatal error: Object of class WP_Error could not be converted to string in /wp-content/plugins/wordpress-seo/frontend/class-frontend.php on line 735. Any idea how to fix this? –  Chris May 17 '13 at 18:56
    
Interestingly enough, I got the same problem today, but only as I tried to set category_name to '' and filter with cat. I couldn't solve it by any other means than sticking with category_name. –  Virgil Dupras May 17 '13 at 19:17
    
I am not sure I follow you here? What did you change exactly? –  Chris May 18 '13 at 11:38
    
Bump, anyone know what's wrong? –  Chris May 23 '13 at 11:35

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.