WordPress REST get all items without a taxonomy assigned

by Ralph Segi   Last Updated September 02, 2019 12:08 PM

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



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

Ralph Segi
Ralph Segi
September 02, 2019 11:56 AM

Related Questions


Create taxonomy with meta term using the WP Rest Api

Updated December 29, 2016 08:03 AM


WP REST API - Add post with taxonomy using term_name

Updated October 25, 2018 10:08 AM