WP API Multiple Taxonomy Query

Before we get started, you will need to be running at least the following:

WordPress: 4.4+
WP REST API (Plugin): 2.0+

The following examples will use a Custom Post Type named rental. When registering the CPT, we need to add the following argument: ‘show_in_rest’  => true. This allows us to query for our custom post type using the following endpoint: /wp-json/wp/v2/rental.

We will also be using a Custom Taxonomy name amenities. Remember to specify the post type (rental) when registering the taxonomy.

 

WP API Query with taxonomy

Objective: Get rentals that have a dishwasher. Solution: GET @ /wp-json/wp/v2/rental?filter[amenities]=dishwasher Our first query is pretty straight forward (and not hard to figure out from the docs). This fetches our rental custom post types that have the amenities taxonomy of dishwasher.

WP API Query with multiple taxonomies (Take 1)

Objective: Get rentals that have a dishwasher and a dryer. Solution (incorrect):  GET @ /wp-json/wp/v2/rental?filter[amenities]=dishwasher,dryer Solution (incorrect):  GET @ /wp-json/wp/v2/rental?filter[amenities]=dishwasher&filter[amenities]=dryer Our first attempt almost worked, but instead of getting rentals that have both a dishwasher and a dryer we get back rentals that have either a dishwasher or a dryer. You can see that behind the scenes WP REST API is using an OR operation. If you are familiar with WP_Query at all, there is an argument that allows you to modify this behavior (see relation in taxonomy parameters). At the time of this writing, it does not appear that we can easily modify this relation through WP REST API (if there is, please inform me!). Here is my hack, I mean solution.
function custom_query_vars( $vars ) {
    array_push( $vars, 'tax_query' );
    return $vars;
}
add_action( 'rest_query_vars', 'custom_query_vars' );
This first piece of code allows us to add the tax_query argument to the WP REST API query.
function custom_rest_query( $args, $request ) {
    if ( $args[ 'amenities' ] ) {
        $tax_query = array(
            'relation' => 'AND'
        );
        $amenities = split( ',', $args['amenities'] );  // NOTE: Assumes comma separated taxonomies
        for ( $i = 0; $i < count( $amenities ); $i++) {
            array_push( $tax_query, array(
                'taxonomy' => 'amenities',
                'field' => 'slug',
                'terms' => array( $amenities[ $i ] )
            ));            
        }
        unset( $args[ 'amenities' ] );  // We are replacing with our tax_query
        $args[ 'tax_query' ] = $tax_query;
    }
    return $args;
}
add_action( 'rest_rental_query', 'custom_rest_query', 10, 2 );
This last piece of the puzzle is actually pretty easy to understand as well. First we tie into the rest_rental_query hook which allows us to modify the query before it happens. The goal here is to add our own tax_query argument to the WP_Query. If we see our amenities filter we create the tax_query (note that we used a comma as our delimiter) and add the argument ‘relation’ => ‘AND’. And thats it! Let’s see what our final query looks like.

WP API Query with multiple taxonomies (Take 2)

Objective: Get rentals that have a dishwasher and a dryer. Solution:  GET @ /wp-json/wp/v2/rental?filter[amenities]=dishwasher,dryer Note that our endpoint did not change. We can now use WP REST API to make multiple taxonomy related queries using the AND relation/operator! You can use this method to easily create more advanced queries outside of WP REST API’s current scope. If you’re looking for web development help, you can always contact us!