Created
January 2, 2019 19:20
-
-
Save Zodiac1978/1855552528b877d6b058688992e706c5 to your computer and use it in GitHub Desktop.
Exclude ALL posts from sub categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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