Skip to content

Instantly share code, notes, and snippets.

@Zodiac1978
Created January 2, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zodiac1978/1855552528b877d6b058688992e706c5 to your computer and use it in GitHub Desktop.
Save Zodiac1978/1855552528b877d6b058688992e706c5 to your computer and use it in GitHub Desktop.
Exclude ALL posts from sub categories
<?php
// https://wordpress.stackexchange.com/questions/64701/exclude-all-posts-from-sub-categories
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) );
}
}
add_action('pre_get_posts', 'filter_out_children');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment