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
- query multiple taxonomy and show post count
- How navigation works in custom loop within shortcode?
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.