# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Block Editor](https://wpide.com/library/category/gutenberg/)
- Tags: query-loop, events, custom-fields
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/query-loop-upcoming-by-date-field/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Query Loop Block: Upcoming Items by a Date Field" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
