0

I have a query on a page and I want to prevent it from returning posts in the parent term that have one of the child terms of the parent. For example:

Parent 1

Child 1

  • Post 1
  • Post 2

NOT

Parent 1

  • Post 1

  • Post 2

Child 1

  • Post 1

  • Post 2

Here's the query I'm currently using, but I haven't figured out how to prevent repetition of posts at the parent level.

<?php
$taxonomy = 'seo_resource_topics';
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) : 
  echo '<h3>'. $term->name. '</h3>';

  $resourcequery = new WP_Query(array(
    'post_type' => 'seo_resource',
    'taxonomy' => $taxonomy,
    'term' => $term->slug,
    'posts_per_page' => -1
    ));

if( $resourcequery->have_posts() ): while( $resourcequery->have_posts() ) : $resourcequery->the_post(); 
?>

Thanks.

1 Answer 1

1

If your post have always children and parent category you can just skip the query for parent using continue:

foreach( $terms as $term ) :
  echo '<h3>'. $term->name. '</h3>'; 
  if ( $term->parent == 0 ) continue;
  // rest of your code here

Problem whith this is only that if a post has a parent term, but doesn't have any children term you'll never see that post.

1
  • Wonderful. Many thanks. I have no trouble enforcing that all posts have to be in the child categories (they were anyway). Commented Oct 15, 2013 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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