1

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!

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

2 Answers 2

4

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');
4
  • 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?
    – chrismaaz
    Commented May 17, 2013 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. Commented May 17, 2013 at 19:17
  • I am not sure I follow you here? What did you change exactly?
    – chrismaaz
    Commented May 18, 2013 at 11:38
  • Bump, anyone know what's wrong?
    – chrismaaz
    Commented May 23, 2013 at 11:35
3

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.

13
  • 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
    Commented Nov 3, 2012 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
    Commented Nov 3, 2012 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
    Commented Nov 3, 2012 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
    Commented Nov 3, 2012 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
    Commented Nov 3, 2012 at 17:34

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.