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

时间:2011-08-24 作者:robertwbradford

假设我有以下分类术语:

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的帖子。

谢谢

4 个回复
最合适的回答,由SO网友:robertwbradford 整理而成

查看/WP includes/taxonomy中的WP\\u Tax\\u查询类。php中,我发现有一个默认为true的“include\\u children”选项。我用以下内容修改了我原来的get\\u 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
    )
  )
));
更多查询参数列表:https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

SO网友:helgatheviking

前几天刚遇到这个:

$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/

SO网友:Muddasir

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; ?>  
SO网友:Markus

运算符“IN”使用,并且它可以工作

\'分类\'=>\'集合\',\'terms\'=>数组(28),\'field\'=>\'id\',\'operator\'=>\'IN\'

结束

相关推荐

如何连接Get_Terms()以仅显示具有自定义元数据的帖子的计数

以下是我所拥有的:一种自定义帖子类型,在存储帖子到期日期的帖子中添加了自定义元值。当帖子过期后,它将不再显示在网站上。这是可行的,但我用这样的方式列出了自定义帖子类型的术语:$termcats = get_terms(\'dcategory\', \'hide_empty=0&orderby=name&pad_counts=1\'); 我正在显示列表中的帖子数量,但这里的问题是,无论帖子是否过期,该数量都会显示所有帖子。例如,我有一篇文章test 那篇文章已经过期了。上面的代码显