Have parent category contain only one post?

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

Related posts

Leave a Reply

1 comment

  1. 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));
       }
    }