2

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

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

<?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!!

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.

  • Just wondering why this doesn't appear on the wordpress documentation? Is it already up since 3.2 ? BDW. This is the right answer.. – neil.odiaz Sep 19 '13 at 0:40
  • See edit. It is documented. – mrwweb Sep 19 '13 at 13:37

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.