How can I combine meta_query queries?

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] => >=
                )

        )

)

Related posts

Leave a Reply

1 comment

  1. 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 );