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

custom taxonomy - tax_query: Don’t show posts with parent term when they have a corresponding child term applied

programmeradmin0浏览0评论

I have a hierarchical taxonomy download-category from which I only want the posts with a parent term to show up:

$downloads_parent_posts = get_posts( array(
    'post_type'        => 'downloads',
    'orderby'          => 'title',
    'order'            => 'ASC',
    'tax_query' => array(
      array(
        'taxonomy'         => 'download-category',
        'terms'            => $term->term_id,
        'field'            => 'term_id',
        'include_children' => false
      )
    ),
) );

This query works fine.

Now what I’d like to achieve is that only posts show up, that don’t have a child of the corresponding parent term applied.

For example I have a parent taxonomy term 'film' and a child term 'horror'. A post has both the parent term and the child term. This post should not show up in that query above.

Is it somehow possible to extend the query or add a condition which filters out the posts with a corresponding child term to the parent term?

Any help would be much appreciated.

I have a hierarchical taxonomy download-category from which I only want the posts with a parent term to show up:

$downloads_parent_posts = get_posts( array(
    'post_type'        => 'downloads',
    'orderby'          => 'title',
    'order'            => 'ASC',
    'tax_query' => array(
      array(
        'taxonomy'         => 'download-category',
        'terms'            => $term->term_id,
        'field'            => 'term_id',
        'include_children' => false
      )
    ),
) );

This query works fine.

Now what I’d like to achieve is that only posts show up, that don’t have a child of the corresponding parent term applied.

For example I have a parent taxonomy term 'film' and a child term 'horror'. A post has both the parent term and the child term. This post should not show up in that query above.

Is it somehow possible to extend the query or add a condition which filters out the posts with a corresponding child term to the parent term?

Any help would be much appreciated.

Share Improve this question edited Oct 13, 2019 at 7:36 majick 5,1412 gold badges18 silver badges30 bronze badges asked Oct 12, 2019 at 8:59 user1706680user1706680 6943 gold badges13 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

I can see you are trying to be clear but still a really confusingly worded question, broke my brain a bit. I think this might achieve what you want, but I'm not fully sure if I understand you correctly or not. Try it out anyway, it may or may not work:

$downloads_parent_posts = get_posts( array(
    'post_type'        => 'downloads',
    'orderby'          => 'title',
    'order'            => 'ASC',
    'tax_query' => array(
      'relation' => 'AND',
      array(
        'taxonomy'         => 'download-category',
        'terms'            => $term->term_id,
        'field'            => 'term_id',
        'include_children' => false
      ),
      array(
        'taxonomy'         => 'download-category',
        'terms'            => get_term_children($term->term_id, $term->taxonomy),
        'field'            => 'term_id',
        'operator'         => 'NOT IN',
      )
    ),
) );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论