0

I wish to list custom taxonomy terms as titles and custom posts as lists under titles:

Basics

  • List item1
  • List item2

Movies

  • List item3

Images

  • List item4

However, custom taxonomy is HIERARCHICAL and if the hierarchy looks like Basics/Movies and Basics/Images, all posts from Movies and from Images are listed also under Basics. So, current result looks like this:

Basics

  • List item1
  • List item2
  • List item3 - wrong!
  • List item4 - wrong!

Movies

  • List item3

Images

  • List item4

So, the actual code is here (simplified):

$cats = get_categories(array('taxonomy'=>'dzialy'));
foreach($cats as $cat){
    $data[] = array(
      'cat'   => $cat,
      'post'  => get_posts(array(
        'post_type' => 'tutorial',
        'numberposts'=> -1,
        'hierarchical' => false,
        'tax_query' => array(
          array(
            'taxonomy' => $cat->taxonomy,
            'field'    => 'slug',
            'terms'    => array($cat->slug),
            'operator' => 'IN'
          )
        )
      ))
    );
}

Now, I display them using $data array, having ['cat'] as category data and ['post'] as complete post data. The post number in ['cat'] array is correct. The post list in ['post'] part contains redundant posts. In all those data there is no indication that post is going from category which is hierarchical or not, no parent information. How to create a 'pure' list? Every post is only in one custom category.

1

Have a look at the Taxonomy Parameters for WP_Query. The argument you are missing is include_children, which by default is true, you need to set it to false to not include posts in child terms.

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.