WordPress REST get all items without a taxonomy assigned

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

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
Add a comment  | 

1 Answer 1

Reset to default 1

I 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 :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745193334a4615971.html

相关推荐

  • WordPress REST get all items without a taxonomy assigned

    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

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信