WordPress: Randomize posts in a category page

wordpress-logo-notext-rgb

The following lines of code change the way of displaying posts in a category page from default to date to random order. Please add following lines of code to functions.php


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

Summary
Article Name
WordPress: Randomize posts in a category page
Description
Code to change the way of displaying posts in a category page from default to date to random order
Author