Query Loop Block: Upcoming Items by a Date Field
Shows only items whose date field (such as an event date) is today or later in any Query Loop block with the class "wpide-upcoming", sorted soonest first.
Opens WPIDE on
Code
// Custom field holding the date (Ymd).
$wpide_query_date_field = 'event_date';
$wpide_query_upcoming = array( 'active' => false );
add_filter( 'pre_render_block', function ( $pre, $parsed ) use ( &$wpide_query_upcoming ) {
if ( 'core/query' === $parsed['blockName'] ) {
$classes = isset( $parsed['attrs']['className'] ) ? ' ' . $parsed['attrs']['className'] . ' ' : '';
$wpide_query_upcoming['active'] = false !== strpos( $classes, ' wpide-upcoming ' );
}
return $pre;
}, 10, 2 );
add_filter( 'query_loop_block_query_vars', function ( $query ) use ( &$wpide_query_upcoming, $wpide_query_date_field ) {
if ( ! $wpide_query_upcoming['active'] ) {
return $query;
}
$meta_query = isset( $query['meta_query'] ) ? (array) $query['meta_query'] : array();
$meta_query[] = array(
'key' => $wpide_query_date_field,
'value' => wp_date( 'Ymd' ),
'compare' => '>=',
'type' => 'NUMERIC',
);
$query['meta_query'] = $meta_query;
$query['meta_key'] = $wpide_query_date_field;
$query['orderby'] = 'meta_value_num';
$query['order'] = 'ASC';
return $query;
} );
Notes
The field must store Ymd dates (20261231), the ACF date picker default. Set the field name at the top.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.