Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Say I have the following taxonomy terms:

Term 1
  Term 1.1
  Term 1.2
Term 2
  Term 2.1

How can I get only posts that are assigned to Term 1 and not include those that are assigned to Term 1.1 or Term 1.2?

For example:

$pages = get_posts(array(
  'post_type' => 'page',
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'taxonomy-name',
      'field' => 'id',
      'terms' => 1 // Where term_id of Term 1 is "1".
    )
  )
);

is also giving me posts that have Terms 1.1 and 1.2 assigned.

Thanks.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

In looking at the WP_Tax_Query class in /wp-includes/taxonomy.php, I found that there is a 'include_children' option which defaults to true. I modified my original get_posts() call with the following, and it works great:

$pages = get_posts(array(
  'post_type' => 'page',
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'taxonomy-name',
      'field' => 'id',
      'terms' => 1, // Where term_id of Term 1 is "1".
      'include_children' => false
    )
  )
);

(Still looking for some complete documentation on the 'tax_query' option...)

share|improve this answer

just came across this the other day:

$tax = 'music';
$oterm = 'pop';
$term = get_term_by('slug', $oterm, $tax);
$termChildren = get_term_children($term->term_id, $tax);
$wp_query = new WP_Query();
$wp_query->query(
    array(
        'posts_per_page' => '5',
        'tax_query' => array(
            array(
                'taxonomy' => $tax,
                'field' => 'slug',
                'terms' => $oterm
            ),
            array(
                'taxonomy' => $tax,
                'field' => 'id',
                'terms' => $termChildren,
                'operator' => 'NOT IN'
            )
        )
    )
);

source: http://return-true.com/2011/08/wordpress-display-posts-from-a-term-without-displaying-posts-from-child-terms/

share|improve this answer

is used the operator 'IN' and it works

'taxonomy' => 'collections', 'terms' => array( 28 ), 'field' => 'id', 'operator' => 'IN'

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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