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.

There's something quite odd happening on a site I'm working on I can't figure out why at all. I have a custom post type set up for news and categories so the user can select what type of news it is (e.g. specials, offers etc).
On the news archive I have a sub-menu set up linking to all the categories so the user can filter the news items by category.

I have a category.php file set up and a function in the functions file that filters the news items on the fly:

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

But for some reason when you're on a category archive page, e.g. www.website.com/news/category/offers the sub-menu breaks, and instead of being the categories list, it's now displaying a list of all page links on the site, e.g. Home, About, Gallery, Contact etc and I can't figure out at all why this is happening, is there a solution to this? Any suggestions would be greatly appreciated!

share|improve this question
    
Have you recently changed themes or moved the website somewhere? Sometimes menus fall out of place during moves like this. Make sure your saved menus are in their proper slots. –  GhostToast Jun 5 '13 at 21:17

1 Answer 1

up vote 1 down vote accepted

Try this

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');

Read how to customize wordpress query

share|improve this answer
2  
the reason why this would work is that is_category() will match any query on a category page, including queries for menu items. you need to check the specific query currently being performed via the query object passed to the function, in this case $qry. –  Milo Jun 5 '13 at 22:26
    
@Ravs Thank you, both! That worked great, cheers for your help, much appreciated. –  user1374796 Jun 5 '13 at 22:39

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.