How to Display Randomly Selected Posts in WordPress

Everyone tells you that tossing a “random posts” widget into your sidebar is the ultimate hack for keeping visitors clicking. So, you look for a way to do it.

The good news is that WordPress has a native database query built exactly for this. You don’t need to install third-party add-on just to shuffle your content. You can even filter the output by specific categories or tags.

But before we look at the code, we need to talk about what actually happens to your server when you run it.

The Harsh Reality of MySQL RAND()

Let’s get technical for a second. WordPress handles random selection by firing off the MySQL RAND() function.

Sure, typing 'orderby' => 'rand' takes two seconds. But for your database, it’s a nightmare. If you want to show five random posts, the server has to assign a random number to every single piece of content you have ever published, sort that entire massive pile, and then hand you the top five.

If your site has fifty articles, you won’t notice. If you have five thousand? You are going to spike your CPU and drag your page load times into the dirt. Use this feature sparingly. And for the love of god, configure your caching properly before you deploy it.

With that warning out of the way, here is how you build it cleanly.

Method 1: The Basic Random List

Forget about the old query_posts() function. It breaks pagination and causes headaches you don’t have time to fix. We are using the WP_Query class.

Drop this snippet into a custom template or a sidebar widget to pull five random posts. It’s clean and gets the job done.

<ul class="random-posts-list">
    <?php 
    $random_args = array(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'posts_per_page' => 5, // Increase or decrease this number
        'orderby'        => 'rand',
        'ignore_sticky_posts' => true,
    );

    $random_query = new WP_Query( $random_args );

    if ( $random_query->have_posts() ) :
        while ( $random_query->have_posts() ) : $random_query->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                </a>
            </li>
        <?php endwhile; 
        wp_reset_postdata(); // Always restore global post data
    else : ?>
        <li>No posts found.</li>
    <?php endif; ?>
</ul>

Method 2: Target Specific Categories

Maybe you only want to shuffle posts from a specific topic. You just need to pass the category IDs into the array. Swap out the numbers in the cat parameter below with your actual IDs.

<ul class="random-category-posts">
    <?php
    $args = array(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'cat'            => '2, 6, 17', // Replace with your category IDs
        'orderby'        => 'rand',
        'posts_per_page' => 5,
        'ignore_sticky_posts' => true,
    );

    $my_query = new WP_Query( $args );

    if ( $my_query->have_posts() ) :
        while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile;
        wp_reset_postdata();
    endif; ?>
</ul>

Method 3: Dynamic Category Shuffling (For Archives)

If a user is reading through your “Marketing” archive, it makes sense to only show them random marketing posts. You don’t need to hardcode a dozen different sidebars to make this happen.

We just use get_queried_object_id() to detect where the user is sitting, and feed that right back into the query.

<?php if ( is_category() ) : ?>
    <div class="dynamic-random-posts">
        <h3>More from this Category</h3>
        <ul>
            <?php 
            // Get the ID of the category archive we are currently viewing
            $current_cat_id = get_queried_object_id(); 
            
            $archive_args = array( 
                'cat'            => $current_cat_id,
                'posts_per_page' => 10, 
                'orderby'        => 'rand',
                'ignore_sticky_posts' => true,
            ); 
            
            $archive_query = new WP_Query( $archive_args ); 
            
            if ( $archive_query->have_posts() ) :
                while ( $archive_query->have_posts() ) : $archive_query->the_post(); ?> 
                    <li>
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </li> 
                <?php endwhile;
                wp_reset_postdata();
            endif; ?>
        </ul>
    </div>
<?php endif; ?>

The Visual Route: Native Blocks

If you rely on the Block Editor, you can skip the PHP entirely.

Drop a native Query Loop block onto your page. Look at the settings panel on the right. Under the ordering options, select “Random.” That is literally it.

Keep your queries light, watch your server resources, and stop relying on heavy third-party software to do what WordPress can handle natively.

If you liked this post, please consider sharing it with your friends:

LinkedIn
X.com
Pinterest

Leave a Comment