I am using wordpress' meta_query to try to build a basic events system.
Each event has a number of meta keys/values. For example:
Start Date
End Date
Ongoing (Yes/No)
I want to separate events that are Ongoing (Ongoing = Yes) and Not Ongoing (Ongoing = No).
Then within my Ongoing events, I want to separate events that are:
My problem lies within case 1 which is, in essence:
(Start Date <= Two Weeks OR End Date <= Two Weeks) AND Ongoing == YES
Using WP_Query, I have the following:
$ongoingSoon = array(
'category_name' => 'event',
'meta_key' => 'End Date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'Start Date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'End Date',
'value' => $nexttwoweeks,
'compare' => '<='
),
array(
'key' => 'Ongoing',
'value' => 'Yes'
)
)
);
Which only returns Ongoing events that Start AND ALSO End within the next two weeks. If I change the relation to OR, then it shows events that are not ongoing as well. Is there a way to achieve what I need?
As meta_query is currently written, I'm 99% sure it's impossible to do this.
What you can do to get around that is to create a (probably private) custom taxonomy for Ongoing that acts like a boolean and only ever uses one term ('yes' or '1' or whatever). Your WP_Query would then be changed to look like this:
$ongoingSoon = array(
'category_name' => 'event',
'meta_key' => 'End Date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Start Date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'End Date',
'value' => $nexttwoweeks,
'compare' => '<='
)
),
'tax_query' => array(
array(
'taxonomy' => 'ongoing',
'field' => 'slug',
'terms' => 'yes'
)
)
);