# Query Loop Block: Most Discussed Posts

> Sorts any Query Loop block with the class "wpide-most-commented" by number of comments, for a "Most discussed" list without a plugin.

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

## Code

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

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

    return $pre;
}, 10, 2 );

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

    $query['orderby']             = array( 'comment_count' => 'DESC', 'date' => 'DESC' );
    $query['ignore_sticky_posts'] = true;

    return $query;
} );
```

## Notes

Add wpide-most-commented to the Query Loop's Additional CSS class(es) and turn off "Inherit query".

## How to use it

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