I’ve been bashing against a bug for a few hours now, and can’t seem to find my error. Could there be a bug with the relation type ‘OR’ in WordPress?
$evenements = new WP_Query(array(
'post_type' => 'evenements',
'orderby' => 'meta_value',
'meta_key' => 'startDate',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'startDate',
'value' => date('Ymd'),
'compare' => '>=',
),
array(
'key' => 'endDate',
'value' => date('Ymd'),
'compare' => '>=',
),
),
));
What is weird is that if I put relation to ‘AND’, it works as intended. If both statements are true, the post will show up. If I put relation to ‘OR’, all posts will show up, and for some reason even the order will not work properly. Even changing ‘order’ from ‘ASC’ to ‘DESC’ won’t change the order the results come in.
To understand the query, here’s the detail of what exactly I’m trying to do:
I want to show events that are in progress or in the future. The two metas are the date at which the event starts (startDate) and the date when the event ends (endDate).
So if the start date is higher or equal (to get if an event is happenning today) to today or the end date is higher or equal to today, show the post.
The second part (with the end date) is made so an event in progress will be shown, and I cant remove the first part (with the start date) because if the event only lasts a day, the client will only enter the startDate meta.
I discarded the bug coming from plugins since I disabled all plugins and the bug was still there.
The
OR
relationship looks fine (so does the meta_query for that fact). I would take a look at the syntax of your compare values (ie. are you sure you want to use>=
for both values). Typically when using a start and end date you want to find the values between those two dates. Your way will find ALL values as startDate typically means before endDate and you want anything AFTER the startDate.So instead of having
startDate >= date(Ymd) <= endDate
you havestartDate || endDate >= date(Ymd)
.