Is there a way to have a meta_query
with sub relations? I don't want one relation
to apply to all of the query's arrays.
I tried this, but to no avail:
'meta_query' => array(
array(
'key' => 'event_type',
'value' => 'session',
'compare' => 'LIKE'
),
array(
'relation' => 'OR',
array(
'key' => 'event_video',
'value' => '0',
'compare' => '>'
),
array(
'key' => 'event_summary',
'value' => '',
'compare' => '!='
)
),
array(
'key' => 'event_date_single',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'event_start_time',
'value' => '',
'compare' => 'LIKE'
)
)
So 'relation' => 'OR'
should only apply between event_video
and event_summary
. Everything else is independent of one another. Is this possible somehow?
Thanks
It looks like this could be a
meta_query
limitation. WP Codex shows that you can use relations for meta queries, but as far as I see the limitation is that you can only use one kind of relation for the wholemeta_query
.This means you can have a full
OR
or a fullAND
(default) relation betweenmeta_query
parameters, but you can never have them both combined, which is just what you want.As I was not glad when I faced it at WP Codex, I went deeper in the matter by manually searching the code. So I found out that
WP_Query
callsWP_Meta_Query
to parsemeta_query
parameters. This isWP_Meta_Query
's class constructor (you can find it inwp-includes/meta.php
at line643
in WP 3.6):Long story short, the second
if
shows that this class will work only either with aOR
orAND
relation, but never with both of them combined.So, unfortunately, it seems that it's not possible to achieve what you are trying to do (at least until WP 3.6). I guess your best shot is to split the main query, first doing specific Meta Queries and then applying their results(returned
metas
) to your main query.I know that this is not a solution but an answer. Nevertheless, I hope this may help you to find a solution for this matter.