# Random Order on a Post Type Archive

> Shows the entries of selected post types, such as testimonials or team members, in a random order on each visit.

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: random, post-types, archives
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/random-order-for-post-type-archive/

## Code

```php
// Post types whose archive is shuffled.
$wpide_random_post_types = array( 'testimonial' );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_random_post_types ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_post_type_archive( $wpide_random_post_types ) ) {
        return;
    }

    $query->set( 'orderby', 'rand' );
} );
```

## Notes

Random order works best when all entries fit on one page; with pagination, items can repeat across pages. Page caching keeps the same order until the cache clears.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Random Order on a Post Type Archive" 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/
