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!
event_start
? If you can provide an actual example of anevent_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 usepre_get_posts
to ensure it still shows event posts in the future ).