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:
The question was for WordPress 3.0, but just in case someone has the same question for a more recent version, from WordPress Codex:
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
That seems to be impossible. Please someone correct me if I’m wrong.
The
meta_query
parameter will actually be transformed into aWP_Meta_Query
object, and therelation
verification won’t go deeper inwp-includes/meta.php
, and occurs just once in the top level:A possible solution for this is to build your own JOIN for this query.
And if you need further verifications and rules, you can also use the
posts_where
filter.