Bricks Query Loop: Upcoming Items by a Date Field
Limits the chosen Bricks query loops to items whose date field is today or later and sorts them soonest first, for event and webinar lists.
Opens WPIDE on
Code
// Query loop element IDs and the date field (stored as Ymd).
$wpide_bricks_upcoming_ids = array( 'abcdef' );
$wpide_bricks_upcoming_meta = 'event_date';
add_filter( 'bricks/posts/query_vars', function ( $query_vars, $settings, $element_id ) use ( $wpide_bricks_upcoming_ids, $wpide_bricks_upcoming_meta ) {
if ( ! in_array( $element_id, $wpide_bricks_upcoming_ids, true ) ) {
return $query_vars;
}
$meta_query = isset( $query_vars['meta_query'] ) ? (array) $query_vars['meta_query'] : array();
$meta_query[] = array(
'key' => $wpide_bricks_upcoming_meta,
'value' => wp_date( 'Ymd' ),
'compare' => '>=',
'type' => 'NUMERIC',
);
$query_vars['meta_query'] = $meta_query;
$query_vars['meta_key'] = $wpide_bricks_upcoming_meta;
$query_vars['orderby'] = 'meta_value_num';
$query_vars['order'] = 'ASC';
return $query_vars;
}, 10, 3 );
Notes
The date field must store Ymd values (ACF date picker default). Set the element IDs and 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.