例如:
Term 1
Term 1.1
Term 1.2
Term 2
Term 2.1
也给我分配了条款1.1和1.2的帖子。谢谢。
#1 楼
在/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
)
)
));
更多查询参数的列表:https://developer.wordpress.org/reference/classes/wp_query/#taxonomy参数
#2 楼
前几天刚遇到:$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一个术语,没有显示来自儿童术语的帖子/
#3 楼
这是完整的代码,希望对您有所帮助。谢谢<?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; ?>
#4 楼
用作运算符'IN'并有效'taxonomy'=>'collections',
'terms'=> array(28),
'field'=> 'id',
'operator'=>'IN'
评论
从链接到的法典页面上读取,我认为tax_query数组中“字段”的值应为“ term_id”而不是“ id”:“可能的值为“ term_id”,“ name”和“ slug”。默认值为'term_id'。”我猜'id'只起作用,因为它会导致回退到默认值。
–贾尼·乌西塔洛(Jani Uusitalo)
2015年9月26日在9:22