Display only posts from a specific parent category

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.

Read More

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

Related posts

1 comment

  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.

Comments are closed.