Archived topic
On a Category Archive page, how can I display the posts randomly
3 replies · Started by Cleo on August 7, 2021
I have a set of posts in a particular category "Oyster Farms". One of my primary menu items goes directly to that category page (an archive page with only posts in the category Oyster Farms). Instead of the default (time post was created) order, I would just like the posts to come up in random order.
The closest similar question I could find about this was someone who wanted to apply random order to a custom archive page.
Hi Cleo,
You can give this php snippet a try:
add_action( 'pre_get_posts', 'generate_random_category_posts', 100 );
function generate_random_category_posts( $query ) {
if ( $query->is_category('Oyster Farms') && $query->is_main_query() ) {
$query->set( 'orderby', 'rand' );
}
}
Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know :)
Worked perfectly. Thank you so much for your quick reply!
You are welcome :)