I have a category structure similar to the below:

  • Grand parent cat
    • Parent cat 1
      • Child cat
      • Child cat
      • Child cat
      • Child cat
    • Parent cat 2
      • Child cat
      • Child cat
      • Child cat

If I query Parent cat 2 for posts, I get all posts in that category AND its child categories.

How do I query the Parent cat and exclude posts in its children?

1 Answer
1

$the_category_id    = 10;
$new_posts          = new WP_Query( array(
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'tax_query'     => array(
        array(
            'taxonomy'          => 'category',
            'terms'             => array( $the_category_id ),
            'field'             => 'term_id',
            'operator'          => 'AND',
            'include_children'  => false
        )
    )
) );

So basically the difference is include_children parameter.

Leave a Reply

Your email address will not be published. Required fields are marked *