Question: I am curious whether Wordpress is able to run nested `meta_query`, with each having different relation keys? As of Wordpress 3.0, `tax_query` is able to perform this function; I'm wondering whether this has an equivalent with `meta_query`.

```
$results = query_posts( array(
    'post_type' => 'event_id',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'relation' => 'OR',
            array(
                'key' => 'primary_user_id',
                'value' => $user_id
            ),
            array(
                'key' => 'secondary_user_id',
                'value' => $user_id
            )
        ),
        array(
            'key' => 'date',
            'value' => array( $start_date, $end_date ),
            'type' => 'DATETIME',
            'compare' => 'BETWEEN'
        )
    )
) );

```

References:

* [WP\_Query Custom Field Parameters - Multiple Custom Field Handling](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)
* [query multiple taxonomy and show post count](https://wordpress.stackexchange.com/questions/23995/query-multiple-taxonomy-and-show-post-count)
* [How navigation works in custom loop within shortcode?](https://wordpress.stackexchange.com/questions/70010/how-navigation-works-in-custom-loop-within-shortcode)

Answer: 
The question was for Wordpress 3.0, but just in case someone has the same question for a more recent version, from Wordpress Codex:

> 
> "Starting with version 4.1, meta\_query clauses can be nested in order to construct complex queries."
> 
> 
> 

<https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters>
So, that query should work on the current Wordpress version.
Meanwhile this is possible, see documentation with example and explanation:

Old link: <https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters>
Update: 2021, new link: <https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters>

and another example <https://wordpress.org/support/topic/wp_query-with-multiple-meta_query/#post-9410992>

```
'meta_query'  => array(
    'relation' => 'OR',
    array(
        'relation' => 'AND',
        array(
            'key'     => '_price',
            'value'   => 1,
            'compare' => '>=',
            'type' => 'DECIMAL',
        ),
        array(
            'key'     => '_price',
            'value' => 3000,
            'compare' => '<=',
            'type' => 'DECIMAL',
        ),
    ),
    array(
        'relation' => 'AND',
        array(
            'key'     => '_price',
            'value'   => 3001,
            'compare' => '>=',
            'type' => 'DECIMAL',
        ),
        array(
            'key'     => '_price',
            'value' => 6000, //fixed <= to =>
            'compare' => '<=',
            'type' => 'DECIMAL',
        ),
    )
),

```