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.
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.)

share|improve this question
add comment

1 Answer

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));
   }
}
share|improve this answer
    
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 Jun 27 '12 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 Jul 1 '12 at 6:08
add comment

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.