# Query Loop Block: Exclude the Current Post

> Removes the post being viewed from any Query Loop block that has the class "wpide-exclude-current", for "More posts" sections under articles.

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

## Code

```php
$wpide_query_exclude = array( 'active' => false );

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

    return $pre;
}, 10, 2 );

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

    $exclude   = isset( $query['post__not_in'] ) ? (array) $query['post__not_in'] : array();
    $exclude[] = get_queried_object_id();

    $query['post__not_in'] = array_values( array_filter( array_map( 'absint', $exclude ) ) );

    return $query;
} );
```

## Notes

Add wpide-exclude-current under Advanced > Additional CSS class(es) on the Query Loop block, with "Inherit query" turned off.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Query Loop Block: Exclude the Current Post" 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/
