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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.