3

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?

4
$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.

  • 1
    However, for the builtin taxonomy category, include_children parameter is set to false when you query using 'category__and', 'category__in' & 'cat' parameter. But for custom taxonomy, you will need to use that. – Shazzad Dec 10 '13 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.