Advanced Query Logic With Multiple Taxonomies

I have a page on my site that acts as a search tool/search results page for users to find rental spaces. On the left side is a column of checkboxes that let the user filter the results by three separate custom taxonomies (space-types, space-features, space-areas) the right side of the page lists the results after the checkbox form is submitted.

I am trying to properly query the spaces (custom post type space-listings) based on the selected terms in each taxonomy but the logic doesn’t seem to be possible and I’m not sure how else to do it.

Read More

This is the needed logic:
If the space listing has ANY of the space-types and ALL of the space-features that the user has selected AND has ANY of the space-areas that they have selected.

Here is the closest I’ve gotten with my query array:

$searchspace = array (
'post_type'=>'space_listing',
'posts_per_page'=> 3,
'paged' => $paged,
'tax_query'=> array (
    'relation' => 'AND',
    array (
        'taxonomy' => 'space-types',
        'field' => 'slug',
        'terms' => $searchedtypes, //Array of selected types
                    'operator' => 'IN'//default
        ),
    array (
        'taxonomy' => 'space-features',
        'field' => 'slug',
        'terms' => $searchedfeatures, //Array of selected features
                    'operator' => 'AND'
        ),
            array (
                    'taxonomy' => 'space-areas',
        'field' => 'slug',
        'terms' => $searchedareas, //Array of selected areas
                    'operator' => 'IN'//default
        ),
    )

);

Related posts

Leave a Reply

1 comment

  1. See Taxonomy Parameters in Codex.

    First — areas should probably be in your tax_query as well.

    Second — you probably need to make use of operator argument in queries (inner ones). I am not sure without testing this out but AND seems like it would for more strict matches than default IN for your mandatory types/features logic.