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.

1

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',
      )
    ),
) );
|improve this answer|||||
  • Sorry for breaking you brain … English is not my first language and sometimes it’s a bit difficult to explain things properly. But many thanks for helping and pointing my brain in the right direction. :D I just needed to change the line 'terms' => get_term_children($term), to 'terms' => get_term_children( $term->term_id, $taxonomy ). Now your code works perfectly! Thanks a lot! – user1706680 Oct 14 '19 at 10:01
  • All good it needed the challenge. You did well to describe it in a few different ways, I just had to reread it quite a few times to really get it, it's not an easy query to get one's head around. Glad it actually worked! I edited it to add the taxonomy. :-) – majick Oct 14 '19 at 11:07
  • Thanks – I really appreciate your effort. Just one last thing: I also needed to change $term to $term->term_id in the same line. – user1706680 Oct 14 '19 at 11:38
  • you're welcome. I come on here for questions to challenge myself with. :-) the get_term_children function description in the codex implies it will accept a term object as the first argument but whatever works. – majick Oct 14 '19 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.