I am trying to refine search in wordpress template. Basically I want to use multiselect with . I am using array to store selected options like this: location[]=term1&location[]=term2 etc etc. This is how the search code looked:
$form_location = $_GET['location'];
if ($form_location != "")
$form_location = array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => $form_location
);
// Final Query
$items_args = array(
'post_type' => 'listings',
'meta_query' => array(
$form_price,
$form_area_range,
$rentbuy
),
'tax_query' => array(
'relation' => 'AND',
$form_location,
$form_property_type,
$form_rating,
$form_bed,
$form_bath
)
);
?>
<?php $items_args = new WP_Query($items_args); if ( $items_args->have_posts() ) :
…
I modified $location to handle array. I didn’t quite done the loop right because I am not sure how to handle tax_query. Here is what I did. I added foreach:
foreach ($form_location as $loc2) {
$form_location = array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => $loc2
); }
This is the output I cached:
WP_Query Object ( [query_vars] => Array ( [post_type] => listings [meta_query] => Array ( [0] => [1] => [2] => Array ( [key] => rentbuy [value] => rent ) ) [tax_query] => Array ( [relation] => AND [0] => Array ( [taxonomy] => location [field] => slug [terms] => term1 ) [1] => [2] => [3] => [4] => )...
Here is the thing, I want the location taxonomy to be used for a query with OR condition for every item from Array it got, and others from tax_query with AND. Is this possible and how? Google is not friendly tonight.
No, this isn’t possible per default. Your best bet is the
posts_clauses
filter. The answers on the site provide plenty examples for that filter. You might also want to look into using theposts_search
filter.