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

1 Answer
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 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *