0

I'm trying to figure out if it's possible to dynamically filter my custom post types by category via a sub-menu I have set up.

So I've set up a custom post type: news and enables categories that I can assign to each post, e.g. events, specials, exhibitions etc.
Then I've set up a WP menu that links to these categories, currently though I have set-up a separate category template for each category archive.
e.g. category-11.php (category ID: 11) which queries the news posts like so: <?php query_posts('post_type=news&cat=11'); ?>
and
category-12.php (category ID: 12) which queries the news posts like so: <?php query_posts('post_type=news&cat=12'); ?>
and so on and so on...
BUT as I'll be handing this site over, the client wont be able to set up a new template file for every new category they add. So is it at all possible to dynamically filter the categories via the sub-menu using the category.php file instead of setting a new template up each time?
A bit of a long shot, I know. Any suggestions would be greatly appreciated!

3
  • Try this solution wordpress.stackexchange.com/questions/10099/… Commented Jun 5, 2013 at 10:47
  • You want to force your category archives to only show posts from the news post type? Also, there are a lot of related posts to the right under "Related". Do none of those help?
    – s_ha_dum
    Commented Jun 5, 2013 at 12:55
  • @s_ha_dum no, the category archives already show posts from the news post type, that's not a problem. I'm trying to figure out if it's possible to, instead of having a category-CATEGORYID.php file for every category, can this not be achieved dynamically with just the category.php file, thus if the client adds another category the results will be filtered without me having to add another template file for that category. Commented Jun 5, 2013 at 15:02

1 Answer 1

0

What you seem to be asking is relatively easy with a filter on pre_get_posts.

function set_post_type_for_category_archive_wpse_101930($qry) {
  if ($qry->is_main_query() && $qry->is_category()) {
    $qry->set('post_type', 'news');
  }
}
add_action('pre_get_posts','set_post_type_for_category_archive_wpse_101930');

That will hijack your entire category archive. That is why I asked if you "want to force your category archives to only show posts from the news post type". From your response, and your code, it seems like you do.

By doing that, you won't need the category-%n.php files at all, just the category.php file.

5
  • WOW that seems to work perfectly, apologies for the misunderstanding, that was my fault. There is one small problem though, oddly when viewing the filtered posts it seems to remove all my main menus from the page (i.e. main menu & sub menu) and has replaced them with a long list of unstyled links to all pages on the site? Commented Jun 5, 2013 at 16:14
  • I added an is_main_query() condition. See if that helps.
    – s_ha_dum
    Commented Jun 5, 2013 at 16:24
  • seems not :( inspecting the code, what it seems to be doing is turning the 2 menus on the page into a list of links to all pages on the site. i.e. instead of my main menu having the id menu-main and class menu the id has been stripped and thus I am left with a long list of links, same for the sub-menu too. Odd isn't it? Commented Jun 5, 2013 at 16:29
  • I'd check the theme and see what kind of conditions it is using to output the menus. Kinda seems like it is testing for post type, since that is the only thing we've changed. The is_main_query should be in there anyway. My mistake for forgetting it in the first place.
    – s_ha_dum
    Commented Jun 5, 2013 at 16:31
  • yes, I'm not sure, can't seem to see anything that would be causing it to react like that. The menus are inserted into the template like so: <?php wp_nav_menu(array( 'menu' => 'news', 'container' => false )); ?> but instead these are being replaced... Commented Jun 5, 2013 at 16:40

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.