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.
'field' => 'term_id'
in tax_query. – freejack Commented Sep 13, 2019 at 20:34