Archived topic
Display Posts Randomly on Homepage?
7 replies · Started by Daniel on August 28, 2020
How can I display posts randomly on the homepage? I found a code that works but it only randomizes posts on category pages.
https://gist.github.com/generatepress/bcee6fc33a22e578885c
Thanks!
Hi there,
Good find!
Try this instead:
add_action( 'pre_get_posts', 'generate_random_posts', 100 );
function generate_random_posts( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'orderby', 'rand' );
}
}
Thank's it seems to work but it displays double posts sometimes. Any way to fix that?
Hmm I'm not sure unfortunately.
Would this work any better?
https://generatepress.com/forums/topic/randomizing-post-order-on-posts-page/#post-454730
No same thing happens, any other ideas?
Hmm does the same issue happen if you disable the infinite scroll option?
Yes the same thing happens.
That's strange. Any chance you can check with WordPress support to see if they have any ideas?
If they can provide the code that works in a twenty series default theme, it would work in GP as well as the post loop is completely handled by WordPress :)