1
Projects <-contains basic info about projects 
-Proj1 <-detailed info about proj1 
-Proj2 <-detailed info about proj2 
-Proj3 <-detailed info about proj3

I have a post attributed to the parent category 'Projects', and I want only that post to be seen when 'Projects' is clicked. By default, each of the subcategories' posts are being shown when I click 'Projects.' Is there a way to change this default setting, so that no posts from subcategories 'Proj1,' 'Proj2,' and 'Proj3' are seen in 'Projects'?

I was referred to a plugin called Cat Lists Plus, but this completely eliminates the parent category link. (This has also not been released yet.)

1 Answer 1

0

You can alter the posts query by using the pre_get_posts filter, and passing the parameter category__in.

In order to do that, put the following code in your theme's functions.php:

add_action( 'pre_get_posts', 'get_parent_category_posts' );

function get_parent_category_posts($query){

   if (is_category() && is_main_query()){
       $curr_cat = get_query_var('cat');
       $query->set('category__in', array($curr_cat));
   }
}
2
  • Thanks for your help. I tried that and then clicked on 'Projects,' but I am now seeing the subcategories AND the post associated with 'Projects.' Do you know how I can remove the subcategories' posts?
    – mdegges
    Commented Jun 27, 2012 at 18:57
  • @mdegges: That's what the code is supposed to do... Maybe the condition is wrong? Can you confirm that the code is actually executed? You can either run the code in debugger mode to see if it gets to those lines, or write echo statements inside the condition and see if they are written on your screen.
    – Lea Cohen
    Commented Jul 1, 2012 at 6:08

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.