admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 1Have 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.
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 badges1 Answer
Reset to default 1Have 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.
本文标签: custom hierarchical taxonomy and custom post type list contains surplus posts
版权声明:本文标题:custom hierarchical taxonomy and custom post type list contains surplus posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749195612a2330926.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论