Making WP_Query limit results by date before today where date is a meta_query

We have a custom post type, events. This has a custom field called event_start.

In trying to query only past events, I'd like to limit the query with this basic logic:

  • post type is events
  • event_start is not empty
  • event_start is a date before today

For this I've composed what seems to be a straightforward query:

$args = array(
  'post_type'        = 'events',
  'posts_per_page'   = -1,
  'meta_query' = array(
           'relation' = 'AND',
            array(
                'key'     = 'event_start',
                'value'   = $today,
                'compare' = ''
            ),
            array(
                'key'     = 'event_start',
                'compare' = 'EXISTS' 
            ),
            array(
                'key'     = 'event_start',
                'compare' = '!=',
                'value'   = ''
            ),
        ),
);

$query = new WP_Query($args);

But this is returning all the events. What am I missing? Appreciate any pointers, thanks!

Topic meta-query meta-value custom-field Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.