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 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 (
'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


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.

  • Thanks, I completely forgot about the operator perameter. I've edited the question (and the logic, which I typed wrong) with what I think should work. Going to test and see if I'm right.
    – Eckstein
    Apr 7 '14 at 18:18
  • @Eckstein does the query not work after the edit?
    – gmazzap
    Apr 7 '14 at 18:20
  • I'm still having trouble with it, but I'm not sure exactly where the problem is.
    – Eckstein
    Apr 7 '14 at 18:30
  • 1
    Nevermind, it was something elsewhere in my code. This works perfectly.
    – Eckstein
    Apr 7 '14 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.