分配给特定的自定义分类术语而不是该术语的子项的get_posts


18

说我有以下分类术语:

Term 1
  Term 1.1
  Term 1.2
Term 2
  Term 2.1

我怎样才能只获得分配给第1学期的帖子,而不包括分配给第1.1学期或第1.2学期的帖子?

例如:

$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".
    )
  )
);

还给我分配了条款1.1和1.2的帖子。

谢谢。

Answers:


35

在/wp-includes/taxonomy.php中查看WP_Tax_Query类时,我发现有一个'include_children'选项,默认为true。我使用以下命令修改了原始的get_posts()调用,它的效果很好:

$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
    )
  )
));

更多查询参数列表:http : //codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters


3
从链接到的法典页面上读取,我认为tax_query数组中“字段”的值应为“ term_id”而不是“ id”:“可能的值为“ term_id”,“ name”和“ slug”。默认值为'term_id'。” 我猜'id'只起作用,因为它会导致回退到默认值。
Jani Uusitalo

6

前几天刚遇到:

$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'
            )
        )
    )
);

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


1

这是完整的代码,希望对您有所帮助。谢谢

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

0

用于运算符“ IN”,并且有效

'分类法'=>'集合','术语'=> array(28),'字段'=>'id','运算符'=>'IN'

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.