WordPress: Randomize posts in a category page

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' );
}
}


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: