最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Query all posts of a custom taxonomy term

programmeradmin0浏览0评论

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
  • Just wanted to say that you should use 'field' => 'term_id' in tax_query. – freejack Commented Sep 13, 2019 at 20:34
Add a comment  | 

1 Answer 1

Reset to default 1

I 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.

发布评论

评论列表(0)

  1. 暂无评论