How to Display Random Posts in WordPress

Displaying random posts in WordPress website can increase visitors engagement and enable you to show random posts to for content monetization purpose. With the help of random posts in WordPress, you can increase the user’s visiting time and reduce the bounce rate of your posts.

Before jumping into, how you can display random posts in WordPress, let’s find out why we should show random posts on our WordPress website.

Why Random Posts?

WordPress by default shows posts in reverse order (from newest to older) which is called reverse chronology. This will show the latest post first on your WordPress website. Most visitors will not get to see the older posts.

You can utilize your website content to show in random order on your blog sidebar which will display the title and some meta information to visitors.

You can also use the interlinking technique to link your older posts with new ones which also help to improve your On-Page SEO. Random posts also help your visitors to find new content in a meaningful way.

Unfortunately, WordPress doesn’t provide random posts out of the box. There are different approaches you which you can choose to add random posts to your WordPress website.

In this post, we will cover two different techniques to show random posts.

  1. Showing random posts using Code Snippet
  2. Showing random posts using a plugin

Display Random Posts in WordPress using Code Snippet

In this method, you have to add some code in your functions.php file to display random posts. You can access the functions.php file by using an FTP client or using WordPress Dashboard Editor.

To find out more about the functions.php file you can check our full post on WordPress functions.php Hacks.

Open your functions.php file and add below code snippet.

function webwhistles_random_posts() {
	$args = array(
		'post_type' => 'post',
		'orderby'=> 'rand',		// Set post order to random
		'posts_per_page' => 5, // You can change the number of posts you want to show.
	);

	$the_query = new WP_Query( $args );

	if ( $the_query->have_posts() ) {
		$string .= '<ul>';

		while ( $the_query->have_posts() ) {
			$the_query->the_post();
			$string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
		}
		$string .= '</ul>';
		/* Restore original Post Data */
		wp_reset_postdata();
	} else {

		$string .= 'No Posts Found.';
	}
	return $string;
}

add_shortcode('webwhistles-random-posts','webwhistles_random_posts');
add_filter('widget_text', 'do_shortcode');

Above block of code will add the random posts functionality to your website which you can access through [webwhistles-random-posts] shortcode.

Simply add the [webwhistles-random-posts] shortcode to your widget text area and 5 random posts will be shown on your sidebar area.

To change the number of posts you want to show, simply change posts_per_page argument in the above code snippet.

You can use the same shortcode if you want to display random posts within your post or page in WordPress.

Display Random Posts on Frontpage

If you are trying to add random posts to your WordPress Blog, then keep reading.

As we mentioned above WordPress by default show the most recent post first.

You can also change this behavior and alter the WordPress Query to return random posts even if you are using WordPress for just blogging purpose.

Add below code to your functions.php file.

add_action('pre_get_posts','alter_query');
function alter_query($query) {
if ($query->is_main_query() &&  is_home())
$query->set('orderby', 'rand'); // Set the Order to Show Random Posts
}

Above code snippet will alter the WordPress Query and set it’s orderby argument to random.

After using the above code, your website will start showing random posts instead of reverse chronology order.

Display Random Posts in WordPress using a Plugin

This is the easiest way to add random posts to your WordPress website and recommended if you don’t have any WordPress development knowledge.

There are a lot of plugins on WordPress Repository to achieve the random posts functionality but I like Advanced Random Posts Widget most and tested my self.

First thing first, go to Plugins menu from WordPress dashboard and click on Add New.

Search for Advanced Random Posts Widget in the search bar. Install and activate this plugin and you are ready to go.

Install Advanced Random Posts Widget Plugin

Install Advanced Random Posts Widget Plugin

Once installed and activated, you can find the Random Posts Widget in Appearance->Widgets section.

Advanced Random Posts Widget - Widget

Advanced Random Posts Widget – Widget

Simply add this widget to blog sidebar or where you want to show random posts. Default settings will do for most of the websites but you can customize to your needs.

Advanced Random Posts Widget is a very powerful plugin and will offer your most of the settings you need to add random posts to your WordPress website.

Advanced Random Posts Widget - Settings

Advanced Random Posts Widget – Settings

You can select different post types, show excerpt, show thumbnail, or displays posts from specific categories or tags.

It also offers to add custom HTML to before and after widget. You can also specify the custom CSS for your widget.

Once you have done your customization, don’t forget to click on Save button to save settings. Now visit your website and you will find the random posts.

Random Posts on WordPress Blog

Random Posts on WordPress Blog

Over To You

By adding random posts on your website you can increase visits to your old posts and give some boost to your website ranking.

If you know any other method to add random posts or have any suggestion please do let us know and post your suggestions in the comments box below.

Leave a Reply