How To Display Posts in a Random Order in WordPress

By default, the blog homepage of your WordPress website displays all published posts (not pages) in reverse chronological order, with the most recently published post at the top. This is for the benefit of returning visitors who come to read your latest posts, as well as new visitors who are unlikely to be pleased with having to go through several steps to see your latest posts.

But if you don't like this "sort posts by published date" behavior and need to "sort posts by random order", then select a code given below according to your need and add it into your theme's "functions.php" file.

Code 1:

Sort Homepage Posts Randomly:

function themesdna_sort_homepage_posts_by_random_order( $query ) {
    if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
        $query->set( 'orderby', 'rand' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'themesdna_sort_homepage_posts_by_random_order' );

Code 2:

Sort Homepage, Archive Pages (Category Pages, Tag Pages,...), Search Pages Posts Randomly:

function themesdna_sort_posts_by_random_order( $query ) {
    if( $query->is_main_query() && ! is_admin() && ( $query->is_home() || $query->is_search() || $query->is_archive() ) ) {
        $query->set( 'orderby', 'rand' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'themesdna_sort_posts_by_random_order' );

Note: If you don't like to edit the functions.php file of your theme, you can use a plugin like "Code Snippets" to run custom code snippets on your site.

Our WordPress Themes

Below are some of our premium WordPress themes. View all our free and premium WordPress themes →
GridMini PRO WordPress Theme

GridMini PRO

Do you like the free version of the GridMini WordPress theme? But what if you need more features and options?...

$25.00
MagShow PRO WordPress Theme

MagShow PRO

MagShow PRO is the premium version of MagShow free WordPress Theme with more advanced features. MagShow PRO version has 160+...

$25.00
TextWP PRO WordPress Theme

TextWP PRO

TextWP PRO is an more improved and advanced version of TextWP Free WordPress Theme. When compare with the TextWP free...

$25.00
Simple Grid PRO WordPress Theme

Simple Grid PRO

You'll be pleased with the Simple Grid PRO WordPress theme if you have tried the free version. It is a...

$25.00
GalleryWP PRO WordPress Theme

GalleryWP PRO

If you like GalleryWP free WordPress Theme, you will love the premium version. GalleryWP PRO is an improved version of...

$25.00
CoolWP PRO WordPress Theme

CoolWP PRO

If you like CoolWP free WordPress Theme, you will love the premium version. CoolWP PRO is very user friendly, feature-rich,...

$25.00
GridMax PRO WordPress Theme

GridMax PRO

GridMax PRO is a feature-rich, advanced WordPress theme than its free version. If you like GridMax Free WordPress Theme, then...

$25.00
CuteMag PRO WordPress Theme

CuteMag PRO

CuteMag PRO is an more improved and advanced version of CuteMag Free WordPress Theme. When compare with the CuteMag free...

$25.00

Can't you choose a single theme? Purchase All Themes for $75.

Save money with our low, one-time price for access to all of our 55 WordPress themes.