admin管理员组文章数量:1414605
i am a bit stuck right now. What i want to achieve is doing a request to the api that returns all posts that do not have a taxonomy assigned.
I can do this request to get posts from a specific taxonomy:
wp-json/wp/v2/lagerboxen?my_taxonomy=6
And i know i can exclude this taxonomy as well to get all but those posts:
wp-json/wp/v2/lagerboxen?my_taxonomy_exclude[]=6
But i have posts that actually represent my_taxonomy as an empty array like:
my_taxonomy = []
And i want to get all those posts without the need to exclude ALL other taxonomies in the request. Basically it is possible in WordPress by querying in PHP like so:
'operator' => 'NOT IN'
but i don't really want to write my own REST method just to do this request. Is there a way to achieve this with the default REST possibilities?
I tried already requests like
wp-json/wp/v2/lagerboxen?my_taxonomy_exclude=
wp-json/wp/v2/lagerboxen?my_taxonomy_exclude[]=
None of them work actually.
Thank you
i am a bit stuck right now. What i want to achieve is doing a request to the api that returns all posts that do not have a taxonomy assigned.
I can do this request to get posts from a specific taxonomy:
wp-json/wp/v2/lagerboxen?my_taxonomy=6
And i know i can exclude this taxonomy as well to get all but those posts:
wp-json/wp/v2/lagerboxen?my_taxonomy_exclude[]=6
But i have posts that actually represent my_taxonomy as an empty array like:
my_taxonomy = []
And i want to get all those posts without the need to exclude ALL other taxonomies in the request. Basically it is possible in WordPress by querying in PHP like so:
'operator' => 'NOT IN'
but i don't really want to write my own REST method just to do this request. Is there a way to achieve this with the default REST possibilities?
I tried already requests like
wp-json/wp/v2/lagerboxen?my_taxonomy_exclude=
wp-json/wp/v2/lagerboxen?my_taxonomy_exclude[]=
None of them work actually.
Thank you
Share Improve this question edited Sep 2, 2019 at 11:56 Ralph Segi asked Sep 2, 2019 at 11:22 Ralph SegiRalph Segi 1416 bronze badges 2- Welcome to WordPress.SE. If you found the answer, try posting it as an answer and then accepting it so it is clear this question is solved and in the future others can still follow the answer to help themselves solve the same problem. – Matthew Brown aka Lord Matt Commented Sep 2, 2019 at 11:54
- 1 Sure i´ll do that. Thanks for the hint. – Ralph Segi Commented Sep 2, 2019 at 11:55
1 Answer
Reset to default 1I was able to solve the problem by adding a custom route. Although i had to do the same process in php by first querying all available terms of the type and then exclude them from the query.
$available_terms = array_map( function ( $term ) {
return $term->term_id;
}, get_terms( 'lagerboxen_kategorie' ) );
return get_posts( [
'post_type' => 'lagerboxen',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'lagerboxen_kategorie',
'field' => 'term_id',
'terms' => $available_terms,
'operator' => 'NOT IN'
]
]
] );
Of course it is still interesting if there is a better way :)
本文标签: WordPress REST get all items without a taxonomy assigned
版权声明:本文标题:WordPress REST get all items without a taxonomy assigned 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745193334a2647018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论