WP_Query – Meta_Query with relation ‘OR’ doesn’t work

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'      => 'date_de_debut',
    '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.

Read more here: WP_Query – Meta_Query with relation ‘OR’ doesn’t work

Leave a Reply

Your email address will not be published. Required fields are marked *