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 combined meta_query queries but they don't work. Any ideas how I can implement queries like the example below?

Thanks, Hinnerk

Array
(
    [relation] => AND
    [0] => Array
        (
            [key] => price
            [value] => 1000
            [type] => numeric
            [compare] => <=
        )

    [1] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [key] => roomcount
                    [value] => 3
                    [type] => numeric
                    [compare] => >=
                )

            [1] => Array
                (
                    [key] => bathroomcount
                    [value] => 2
                    [type] => numeric
                    [compare] => >=
                )

        )

)
share|improve this question
2  
Could you please edit your question to show your query and how you build it and not the output of the Array? –  kaiser Nov 16 '12 at 20:28
    
@kaiser: Chip did it below –  Hinnerk Altenburg Nov 19 '12 at 20:26
1  
For future reference: it is much preferred that you include your actual code in your question, rather than var_dump() data or similar output. –  Chip Bennett Nov 19 '12 at 20:38
add comment

1 Answer

Using your constructed array, I think this is the correct syntax:

$meta_query_args = array(
    'meta_key' => 'price',
    'meta_value_num' => 1000,
    'meta_compare' => '<=',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'roomcount',
            'value' => 3,
            'type' => 'numeric',
            'compare' => '>='
        ),
        array(
            'key' => 'bathroomcount',
            'value' => 2,
            'type' => 'numeric',
            'compare' => '>='
        )
    )
);

$realestate_meta_query = new WP_Query( $meta_query_args );

You might also try nesting meta_query one more level (though I don't know if that will work):

$meta_query_args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'price',
            'value' => 1000,
            'type' => 'numeric',
            'compare' => '<='
        ),
        array(
            'relation' => 'OR',
            array(
                'key' => 'roomcount',
                'value' => 3,
                'type' => 'numeric',
                'compare' => '>='
            ),
            array(
                'key' => 'bathroomcount',
                'value' => 2,
                'type' => 'numeric',
                'compare' => '>='
            )
        )
    )
);

$realestate_meta_query = new WP_Query( $meta_query_args );
share|improve this answer
    
Your nested example is the one I've already tried (my array dump is the output of your example), but unfortunately it didn't work. The other example looks good for my very example, but besides price I actually have more keys to combine on that AND level. –  Hinnerk Altenburg Nov 19 '12 at 20:32
    
@HinnerkAltenburg Please edit your question and show your actual query. It's annoying to ask multiple times. There can be that many things that don't work or give unexpected output just because there's a single char different. Copypasteedit for the rescue... –  kaiser Nov 20 '12 at 0:24
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.