Need an SEO Expert? Get Your Free Strategy Session?
Skip to main content

page speed

THE REAL WP RANDOM POSTS FUNCTION CODE

Est. reading time: 1 minutes

Quick Answer: THE REAL WP RAND FUNCTION CODE //Modify the default WP query function wp_custom_query( $query ) { // Make sure we only modify the main query if( $query->is_main_query() && ! is_admin() && $query->is_home() ) { // Set parameters to modify the query $query->set( 'orderby', 'rand' ); $query->set( 'order', 'DESC' ); $query->set( 'suppress_filters', 'true' ); } } // Hook our custom query to the pre_get_posts add_action( 'pre_get_posts', 'wp_custom_query' ); And this wp function code really works :).

THE REAL WP RAND FUNCTION CODE


//Modify the default WP query
function wp_custom_query( $query ) {
 
// Make sure we only modify the main query  
    if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
 
        // Set parameters to modify the query
        $query->set( 'orderby', 'rand' );
        $query->set( 'order', 'DESC' );
        $query->set( 'suppress_filters', 'true' );
    }
}
 
// Hook our custom query to the pre_get_posts 
add_action( 'pre_get_posts', 'wp_custom_query' );

And this wp function code really works 🙂