I'm trying to build a filter function for the main query on a custom post type archive page.

The custom post type is named "Hotel". For this custom post type i have a custom taxonomy named "Facilities". Each "hotel" post can have one or many facilities.

I've build a form function with an checkbox for each available taxonomy. Also I've build an hook to the pre_get_posts function that looks like this:

$facilitati = isset($_POST['fac']) ?
    'taxonomy' => 'facilitati',
    'field' => 'id',
    'terms' => $_POST['fac'],
    'operator' => 'IN'
) :
    'taxonomy' => 'facilitati',
    'field' => 'id',
    'terms' => array( 0 ),
    'operator' => 'NOT IN'

$arr = array(
    'relation' => 'AND',

$query->set('tax_query', $arr );

The problem is that this is not functioning like I want it to. Eg. Hotel 'ABC' has facilities: TV, phone, WIFI, parking place Hotel 'DEF' has facilities: TV, phone, restaurant Hotel 'GHI' has facilities: WIFI, parking place

If i check all the facilities in the form all the hotels get displayed. This makes sense because as I see it the tax_query sees all terms that are in the array of checked facilities.

So for example if hotel 'GHI' has only facilities WIFI and parking place it will also be displayed, because the two facilities find themselves in the array of all facilities.

Is it any possible method to make this filtering? Also I want to be able to obtain the following thing: If only WIFI and parking place facility is checked, the result should not by only hotel 'GHI' but also hotel 'ABC'.

I've searched for a solution on the entire WordPress WP Query Class description but haven't found one.


1 Answer 1


I think your problem is related with the use of the operator argument in your tax_query. Actually you are using IN and NOT IN. From your question I understand you want AND. Also, you don't need set terms aguments to 0 if the $_POST[] is not set:

if(isset($_POST['fac'])) {
    // I assume that $_POST['fac'] is an array with the terms ID
    // because you talked about an array of checkbox inputs in your form
    // also, as you are using the field => 'id' I assume that the value of
    // each checkbox is a term ID
    $terms = array_map('intval', $_POST['fac']);
    $facilitati =  array(
                      'relation' => 'AND',
                           'taxonomy' => 'facilitati',
                           'field'    => 'id',
                           'terms'    => $terms,
                           'operator' => 'AND'
    $query->set('tax_query', $facilitati );

For further help I would need to know the expected data type of $_POST['fac'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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