WordPress: Show random posts

WordPress: Show random posts

Sometimes you want to show a list of random posts from your WordPress blog/website to your readers. It can be a great way of engaging readers for a longer duration by offering them more content. How to show a list of random posts in WordPress?

Copy the code snippet below and paste it to sidebar.php or the desired WordPress theme file where you want to show the list of random posts.

<?php
$my_query = new WP_Query('orderby=rand&showposts=5');
while ($my_query->have_posts()) : $my_query->the_post();

if ( has_post_thumbnail() ): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php echo get_the_post_thumbnail( $post->ID, 'thumbnail', array('class' => 'thumbnail') ); ?>
</a>
<?php endif; ?>

<h3><a href="<?php the_permalink(); ?>" title="<<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php the_time('j M Y'); ?></p>
<p><?php comments_popup_link('0 Comment', '1 Comment', '% Comments'); ?></p>

<?php endwhile; ?>