1 of 2
Eckstein
  • 1.1k
  • 4
  • 27
  • 52

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.

This is the needed logic: If the space listing has ALL of the space-types AND space-features that the user has selected AND also has at least ONE 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,
'space-areas' => $searchedareas, //$searchedareas is an array of areas selected
'tax_query'=> array (
    'relation' => 'AND',
    array (
        'taxonomy' => 'space-types',
        'field' => 'slug',
        'terms' => $searchedtypes, //Array of selected types
        ),
    array (
        'taxonomy' => 'space-features',
        'field' => 'slug',
        'terms' => $searchedfeatures, //Array of selected features
        ),
    )
        
);
Eckstein
  • 1.1k
  • 4
  • 27
  • 52