I have 5 big categories and there are many small categories in them. If I click on the category menu, I can see the posts in order of date. But I want it to be displayed randomly. How can I make a post appear randomly?

1 Answer 1


You could hook into the pre_get_posts action hook and set the posts' order to random only for specific categories. Here's a basic example on how to achieve so:

function sort_posts_randomly( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( is_category( 'category id, slug, or name' ) ) { 
            // Sort the posts randomly
            $query->set( 'orderby', 'rand' );
add_action( 'pre_get_posts', 'sort_posts_randomly' );
  • You're welcome. You can pass an array of categories to is_category(), for example: is_category ( array ( 'slug1', 'slug2' ) ) and so on.
    – Johansson
    Nov 3, 2019 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.