2

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

  • 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 Sep 2 at 11:54
  • 1
    Sure i´ll do that. Thanks for the hint. – Ralph Segi Sep 2 at 11:55
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.