$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom hierarchical taxonomy and custom post type list contains surplus posts|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom hierarchical taxonomy and custom post type list contains surplus posts

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far