Can I use OR relation for one item when calling tax_query from WP_Query and use AND for the rest?

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() ) : 

Read More

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.

Related posts

1 comment

Comments are closed.