admin管理员组

文章数量:1122832

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.

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 asked Aug 24, 2011 at 16:03 robertwbradfordrobertwbradford 1,1191 gold badge10 silver badges16 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 64

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' => 'term_id', 
      'terms' => 1, /// Where term_id of Term 1 is "1".
      'include_children' => false
    )
  )
));

List of more query parameters: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

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/

Here is complete code hope it helps. Thanks

<?php 
$terms_array = array( 
  'taxonomy' => 'services', // you can change it according to your taxonomy
  'parent'   => 0 // If parent => 0 is passed, only top-level terms will be returned
);
$services_terms = get_terms($terms_array); 
foreach($services_terms as $service): ?>
<h4><?php echo $service->name; ?></h4>
<?php 
$post_args = array(
      'posts_per_page' => -1,
      'post_type' => 'service', // you can change it according to your custom post type
      'tax_query' => array(
          array(
              'taxonomy' => 'services', // you can change it according to your taxonomy
              'field' => 'term_id', // this can be 'term_id', 'slug' & 'name'
              'terms' => $service->term_id,
          )
      )
);
$myposts = get_posts($post_args); ?>
<ul>
<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
  <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
<?php endforeach; // Term Post foreach ?>
</ul>
<?php wp_reset_postdata(); ?>

<?php endforeach; // End Term foreach; ?>  

is used the operator 'IN' and it works

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

You can avoid the tax query like this :

$posts = get_posts(
      array(
          'posts_per_page' => -1,
          'post_type' => 'my_post_type',
          'services' => term->name
      )
);

本文标签: getposts assigned to a specific custom taxonomy termand not the term39s children