There’s no need for the custom meta SQL filter – the beauty of storing dates in the format Ymd
is that you can treat them numerically, and MySQL will still be able to find events in a given “range” and sort them ascending/descending.
I’ve done this recently on another site using ACF for start/end date:
if ( ! empty( $_GET['_y'] ) )
$year = absint( $_GET['_y'] );
else
$year = date( 'Y ');
if ( ! empty( $_GET['_m'] ) && in_array( $month = absint( $_GET['_m'] ), range( 1, 12 ) ) )
$month = zeroise( $month, 2 );
else
$month = date( 'm' );
$qryevents = array(
'post_type' => 'events',
'posts_per_page' => 50,
'post_status' => 'publish',
'orderby' => 'meta_value_num', // Ensure order is numerically based
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_start_date',
'compare' => '>=',
'value' => "{$year}{$month}01",
'type' => 'NUMERIC',
),
array(
'key' => 'event_end_date',
'compare' => '<=',
'value' => "{$year}{$month}31", // Doesn't matter if there aren't 31 days in this month, will still work,
'type' => 'NUMERIC',
)
)
);
No need for the overly complex date string calculations, and no need for the filter.