14

How to Show Random Posts on Your WordPress Homepage

Powered by WPCodeBox

This code snippet allows you to display random posts on your WordPress homepage instead of the default reverse chronological order, helping to enhance content discovery.


<?php 

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

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.