custom hierarchical taxonomy and custom post type list contains surplus posts

admin2025-06-06  5

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.

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.

Share Improve this question asked Nov 9, 2018 at 3:03 piotaopiotao 1033 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749195638a317164.html

最新回复(0)