Show Post from parent category (custom taxonomy) ONLY!

I want to display all post under a parent category (custom taxonomy) ONLY!.

Here’s what I’ve done so far:’

Read More
<?php
$args = array( 
'posts_per_page' => 100, 
'post_status'   => 'publish', 
'tax_query'     => array(
    array(
        'taxonomy'  => 'ait-dir-item-category',
        'field'     => 'id',
        'terms'     => 75
    )
), 
'post_type'     => 'ait-dir-item' );
$the_posts = new WP_Query($args);

This displays all the posts including the sub categories, which I don’t want to.

Thanks!!

Related posts

1 comment

  1. Any tax_query can take an include_children argument (see Codex) that defaults to true. So just add that to your code and it should work:

    <?php
    $args = array( 
    'posts_per_page' => 100, 
    'post_status'   => 'publish', 
    'tax_query'     => array(
        array(
            'taxonomy'  => 'ait-dir-item-category',
            'field'     => 'id',
            'terms'     => 75,
            'include_children' => false
        )
    ), 
    'post_type'     => 'ait-dir-item' );
    $the_posts = new WP_Query($args);
    

    Note: I’m unsure of the behavior of include_children when a post is assigned to both a child and parent, but I would guess that the post would show up.


    EDIT: Posted the wrong link earlier. from The Codex:

    include_children (boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.

Comments are closed.