Select Page

Do you want to display a list of random blog posts from your blog? It’s really easy and WordPress provides an easy way to do it. The below code snippet will help you do it.

Place this code snippet wherever in your theme you want to display a list of random blog posts:

<div class='random-posts'>
	<h2>If you liked reading this article, you might also like these other posts from my blog:</h2>
	<ul>
	<?php
		$the_query = new WP_Query(array('numberposts' => 10, 'orderby' => 'rand'));
		if ( $the_query->have_posts() ) {
			while ( $the_query->have_posts() ) {
				$the_query->the_post();
	?>
				<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
	<?php 
			}
		}
	?>
	</ul>
</div>

if you place this code in your theme’s single.php file below the part that displays the content, then your readers will see a list of random posts after every blog post.

The above code displays a list of random blog posts by first querying posts by random order. And then displaying them as list items (<li>) inside an unordered list (<ul>).