Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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']) ?
    array(
    'taxonomy' => 'facilitati',
    'field' => 'id',
    'terms' => $_POST['fac'],
    'operator' => 'IN'
) :
array(
    'taxonomy' => 'facilitati',
    'field' => 'id',
    'terms' => array( 0 ),
    'operator' => 'NOT IN'
);

$arr = array(
    'relation' => 'AND',
    $locatii,
    $facilitati
);

$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.

share|improve this question
    
See this post for setting the tax_query parameter: wordpress.stackexchange.com/questions/35196/… –  helgatheviking Sep 22 '13 at 8:46

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',
                      array(
                           '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'].

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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