admin管理员组文章数量:1415092
I am using this code on single.php
, This query working well but when post has parent term (that parent also have child terms) this give output all posts of parent & children
But in this condition I want it to query only parent term posts.
<?php
$post_terms = wp_get_object_terms($post->ID, 'serial', array('fields'=>'ids'));
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'serial',
'field' => 'id',
'terms' => $post_terms,
),
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
}
} else {
}
wp_reset_postdata();
?>
How to query posts according to attached term, Only attached term (not to check for child terms when post have parent term)?
I am using this code on single.php
, This query working well but when post has parent term (that parent also have child terms) this give output all posts of parent & children
But in this condition I want it to query only parent term posts.
<?php
$post_terms = wp_get_object_terms($post->ID, 'serial', array('fields'=>'ids'));
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'serial',
'field' => 'id',
'terms' => $post_terms,
),
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
}
} else {
}
wp_reset_postdata();
?>
How to query posts according to attached term, Only attached term (not to check for child terms when post have parent term)?
Share Improve this question asked Sep 13, 2019 at 19:20 F.AF.A 255 bronze badges 1 |1 Answer
Reset to default 1I believe your issue is with understanding the nature of posts' hierarchy vs pages' hierarchy. A page is a type of "post". It can have hierarchical structure.
Posts, by default, do not have hierarchy. So, you would not be able to get post-parents without their children, because the system does not recognize a hierarchy. This is true unless you modify the structure with a custom function, or install a specific plugin to add hierarchical structure to Posts.
If you really need that hierarchy, you could try this StackOverflow answer to add a hierarchy. This will permit what you are trying to accomplish.
本文标签: phpQuery all posts of a custom taxonomy term
版权声明:本文标题:php - Query all posts of a custom taxonomy term 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745159308a2645365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'field' => 'term_id'
in tax_query. – freejack Commented Sep 13, 2019 at 20:34