Exclude child categories when getting posts from parent:
use category__in
Here using category id:
$query = new WP_Query( array( 'category__in' => 4 ) );
Excluding children of custom taxonomies:
tax_query
argument of WP_Query
has include_children
set to true by default, so you can set that to false.
include_children (boolean) – Whether or not to include children for
hierarchical taxonomies. Defaults to true.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'include_children' => false,
),
),
);
$query = new WP_Query( $args );
Excluding Children and only getting posts in category 4 with people taxonomy:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4,
'include_children' => false,
),
array(
'taxonomy' => 'people',
),
),
);
$query = new WP_Query( $args );
NOTE: see the Tax_query link above for more nested/complicated queries.