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

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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 the posts_search filter.

share|improve this answer
    
Thanks for your answer. As I am looking into posts_clauses I stumbled upon a code which is supposed to "give you better insights about the whole query parts and an easier way to debug." The code which I found here wordpress.stackexchange.com/a/44904/40782. However when I add it this is what I get pastie.org/8422891, without any search results (there are if I remove the filter). –  user130888 Oct 23 '13 at 1:34
    
I forgot to add return, it shows search results but there seems to be not much in this dump where I can define OR for what I need it –  user130888 Oct 23 '13 at 1:42
    
@user130888 See update –  kaiser Oct 23 '13 at 2:11
    
Thank you very much! –  user130888 Oct 23 '13 at 2:32
add comment

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.