0

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!

1
  • what is the format/data value of event_start? If you can provide an actual example of an event_start post meta value from the database that would be great! Note that querying via post meta is expensive, if you can change this to be the post publish date then that makes this significantly faster, possibly hundreds of times faster ( you can use pre_get_posts to ensure it still shows event posts in the future ).
    – Tom J Nowell
    Apr 11 at 23:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.