Tutorials

How to Display Random Posts in WordPress?

Displaying random posts could be an effective way to increase visitor engagement. You can show random posts to make their visits longer and help them find new content in a simple way.

Display Random Posts

WordPress doesn’t come with any built-in feature to show random posts. Luckily, there are some workarounds to show random posts on WordPress sites. In today’s post, I am going to share two ways to do this –

  1. Showing random posts with a plugin and
  2. Showing random posts manually.

Before we dive into the methods, let’s take a moment to discuss why you should display random posts.

Why Display Random Posts?

The default WordPress setting will display your last post as the first item and so on. This is called reverse chronology. WordPress uses reverse chronology to ensure that your homepage or the blog page always features the latest posts.

While that works great, you also need a way to highlight your existing posts. One way to do that is to follow a solid internal linking strategy. This will direct the visitors to your older posts and help you get more SEO benefits.

Another great way to utilize your existing posts is to show random posts on the sidebar. In comparison with internal linking, this will show the full title and other meta information of the articles. This makes it more likely for the visitors to be interested in these posts.

Now that you know why it’s important to show random posts, let’s find out how to do this.

Grammarly Banner

#1. Displaying Random Posts With a Plugin

There are several WordPress plugins that allow you to show random posts. Among all these plugins, I especially like the Advanced Random Posts Widget plugin. The plugin makes it very simple to display random posts.

As you can guess from the name, this plugin will create a custom widget on your website. Once you install and activate Advanced Random Posts Widget, you will find the widget in the Appearance -> Widgets page.

Advanced Random Posts Plugin

Drag the widget to your desired location. You will find several configuration options. These enable you to define the title, title URL, add a custom CSS class or add custom HTML or text before or after the random posts. In the “Posts” section, you can choose the number of posts to show, which type of content, and so on.

You can choose your desired categories in the “Taxonomy” section. It is also possible to limit to specific tags and taxonomies. Other available options include displaying the thumbnail, defining the width, height, thumbnail alignment, display excerpt, date, etc. There is a separate section for providing Custom CSS too.

Advanced Random Posts Plugin

Manually Displaying Random Posts

In this method, you have to add some code to the functions.php file of your theme. You can access the file by using the file manager provided by your hosting provider, by using an FTP client, or through WordPress dashboard.

Open the file, and add the following code to the file –

function wpdean_rand_posts() {
$args = array(
'post_type' => 'post',
'orderby'=> 'rand',
'posts_per_page' => 5,
);

$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('wpdean-random-posts','wpdean_rand_posts');
add_filter('widget_text', 'do_shortcode');

Inserting this code will add the following shortcode to your theme –

[wpdean-random-posts]

You can place this shortcode in any text widget and display five random posts on any widget-enabled location. If you want to show more or less posts, just change the value of ‘posts_per_page‘ in the code.

Show Random Posts Manually

Over to You

Displaying random articles on the sidebar brings life back to your old posts. Now that you know the best ways to do that choose any of these methods and show off your existing posts. Let me know which method suits your requirements.

How to Display Random Posts in WordPress?
5 (100%) 8 votes

1 Comment

  1. Phil Simon

    This works but I’m finding that, upon refresh, the code doesn’t generate random posts. Any ideas? Thanks.

Leave a Reply

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!