# Query Loop Block: Random Order

> Shows posts in random order in any Query Loop block with the class "wpide-random-order", for rotating testimonials, team members or featured posts.

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

## Code

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

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

    return $pre;
}, 10, 2 );

add_filter( 'query_loop_block_query_vars', function ( $query ) use ( &$wpide_query_random ) {
    if ( $wpide_query_random['active'] ) {
        $query['orderby']             = 'rand';
        $query['ignore_sticky_posts'] = true;
    }

    return $query;
} );
```

## Notes

Add wpide-random-order to the Query Loop's Additional CSS class(es). With page caching the order only changes when the cache is refreshed.

## How to use it

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