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 badges1 Answer
Reset to default 3I 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',
)
),
) );