Created
March 5, 2014 14:27
-
-
Save yoren/9368171 to your computer and use it in GitHub Desktop.
Exclude Subcategories in a Loop
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 | |
function wpse_filter_child_cats( $query ) { | |
if ( $query->is_category ) { | |
$queried_object = get_queried_object(); | |
$child_cats = (array) get_term_children( $queried_object->term_id, 'category' ); | |
if ( ! $query->is_admin ) | |
//exclude the posts in child categories | |
$query->set( 'category__not_in', array_merge( $child_cats ) ); | |
} | |
return $query; | |
} | |
add_filter( 'pre_get_posts', 'wpse_filter_child_cats' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified from http://wpengineer.com/876/exclude-subcategories-in-a-loop/.