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.