Exclude ALL posts from sub categories

Don’t change your template, and please do not use query_posts.

Add this to your function.php:

add_action('pre_get_posts', 'filter_out_children');

function filter_out_children( $query ) {
  if ( is_main_query() && is_category() && ! is_admin() ) {
     $qo = $query->get_queried_object();
     $tax_query = array(
       'taxonomy' => 'category',
       'field' => 'id',
       'terms' => $qo->term_id,
       'include_children' => false
     );
     $query->set( 'tax_query', array($tax_query) );
  }
}

Leave a Comment