# Query Loop Block: Only Posts From the Last 30 Days

> Limits any Query Loop block with the class "wpide-recent" to posts published in the last 30 days, for "This month" or "Fresh this week" sections.

- Type: PHP
- Runs: Run everywhere
- Category: [Block Editor](https://wpide.com/library/category/gutenberg/)
- Tags: query-loop, dates, posts
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/query-loop-recent-days/

## Code

```php
// How many days back to show.
$wpide_query_recent_days = 30;
$wpide_query_recent      = array( 'active' => false );

add_filter( 'pre_render_block', function ( $pre, $parsed ) use ( &$wpide_query_recent ) {
    if ( 'core/query' === $parsed['blockName'] ) {
        $classes = isset( $parsed['attrs']['className'] ) ? ' ' . $parsed['attrs']['className'] . ' ' : '';
        $wpide_query_recent['active'] = false !== strpos( $classes, ' wpide-recent ' );
    }

    return $pre;
}, 10, 2 );

add_filter( 'query_loop_block_query_vars', function ( $query ) use ( &$wpide_query_recent, $wpide_query_recent_days ) {
    if ( ! $wpide_query_recent['active'] ) {
        return $query;
    }

    $date_query   = isset( $query['date_query'] ) ? (array) $query['date_query'] : array();
    $date_query[] = array( 'after' => absint( $wpide_query_recent_days ) . ' days ago', 'inclusive' => true );

    $query['date_query'] = $date_query;

    return $query;
} );
```

## Notes

Change the number of days at the top. Use the class wpide-recent on the Query Loop block.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Query Loop Block: Only Posts From the Last 30 Days" 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/
