How to Display Random Posts in WordPress to Increase Engagement?

Last Updated on   May 19 2023,
  by    
computer desktop wordpress

Introduction

A considerable percentage of website visitors don't browse through the subsequent pages beyond the first page displaying your company's blog. So if you show a static set of articles on the first page, the chances are that visitors would simply never care to browse further. Your articles, therefore, would be useless. Visitors might miss critical updates, company milestones, and announcements which are featured on pages 3, 4, 5 and so on.

WordPress for example, arranges your blog posts in reverse chronological order by default, rendering your older posts with limited visibility (unless visitors browse through the later pages). To grab visitor's interest and ensure repeat visitation to your website, you need to offer more “mixed” content to the viewer. This can be done if you display random posts in your WordPress blogs.

Why Display Random Posts on your website?

As users visit your website and discover the relatively older (archived) articles along with the current one, your page views increase. This also adds to the revenue of the website and converts a casual visitor into a subscriber. Ultimately you also get to improve upon your SEO score, thus ranking your website higher in search engines.

In addition to the SEO element, showing random posts in WordPress will also engage the visitors with awesome content of your website. If you put a lot of efforts and energy in creating a piece of content, then it would be best to show related/random posts, so users get the best juice of content while they browse your website.

How to Display Random Posts in WordPress

WordPress is famous for its endless customization and possibilities. You can display random posts in WordPress with the help of some code tweaks or by installing a plugin. We, however, advise our readers to carefully pick the plugin as it might affect the speed of your website.

Quick Summary table for the top 5 Best Random Posts WordPress Plugins;

Plugin Name Main Features Ease of Use Customisability My View
Advanced Random Posts Widget Customisable widget, multiple widget instances, flexible display options Very Easy High Best for those who need highly customisable random post widgets Try For Free
Yet Another Random Posts Plugin Uses an algorithm for relevance, allows for thumbnail display, widget and shortcode capabilities Easy Moderate Best for sites with a lot of content, as the algorithm finds relevant posts Try For Free
WP Random Post Inside Shortcode generator, fully responsive, lightweight Easy Moderate Best for inserting random posts directly into page/post content Try For Free
Random Post Widget Simple random post display, lightweight, multiple widgets Very Easy Low Best for straightforward random post display in the sidebar Try For Free
Random Posts Plugin Shortcode and PHP function support, custom post type support Easy High Best for those needing support for custom post types Try For Free

How to Display Random Posts in WordPress with Plugins?

The most popular WordPress plugin to show random posts in WordPress is ‘Advanced Random Posts Widget‘ plugin. Navigate to Plugins >> Add new and install the plugin. After activation, you will notice a new widget labeled ‘Random Posts' under the list of available widgets. This is a WordPress random post widget. From the displayed widget settings (as shown below), choose the number of random posts you want to display and click on save.

random-posts-widget
random-posts-widget

The Posts tab shown above contains settings to choose the number of posts to display, how many posts to skip, which type of posts to display and post with what status should be displayed.

The other tabs displayed in the plugin are of the following utility: –

  • The General tab contains basic values such as Title, Title URL, custom CSS Class, and custom HTML Before and After the Posts.
  • The Taxonomy tab contains settings to limit random posts to only specific categories or tags.
  • The Thumbnail tab enables you to display or hide a thumbnail, select its size and alignment.
  • The Extras tab holds settings such as to show a post excerpt date, modified date and to use relative date such as display random posts in WordPress from 5 days ago, etc.
  • The Custom CSS tab is used in case you want to add some custom modifications to how the widget looks on the front end.
random-widget-display
random-widget-display

How to Add Code to Display Random posts in WordPress

To display Random Posts inside another post, a custom short code needs to be created, which allow us to run custom scripts or queries inside a post, page or sidebar.

A sample looks like the following:-

[code]random-posts posts=5 cat=themes,plugins][/code]

It accepts two variables, the number of posts and the category from which these belong to.

We need to create a function that processes the information from the shortcode, retrieves and displays them. You can place these functions inside your theme's functions.php or if you have a site specific plugin, then use that.

We will create a function called random_posts_function() {}and extract the values of posts and categories from the shortcode.

[code]
<?php // The Shortcode's Function. function random_posts_function($atts) { // Extracting Attributes extract(shortcode_atts(array( ‘posts' => 1,
‘cat' => ”,
), $atts));
}
[/code]

After extracting the values, we can create a custom query which asks the WordPress to return the $posts (number of posts) from $cat category in a RAND (random) order.

[code]
<?php // Creating a custom query query_posts( array( ‘orderby' => ‘rand',
‘ignore_sticky_posts' => 1,
‘category_name' => $cat,
‘posts_per_page' => $posts
)
);
[/code]

To display the list of the randomly selected posts, the following code can be used.

[code]
<?php
// Opening an unorderd list (

<ul> )
$return_string = ‘

<ul class="randomPosts">';

// creating a list of returned posts
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= ‘

<li><a href="' . get_permalink() . ‘">' . get_the_title() . ‘</a></li>

‘;
endwhile;

endif;

// closing the opened unordered list ( </ul>

)
$return_string .= ‘</ul>

‘;
[/code]

We now need to reset the WordPress query to enable it to continue to load the page, return the list of posts built in the previous code.

[code]
// resetting the WordPress query
wp_reset_query();

// displaying the list of random posts
return $return_string;
[/code]

Finally we need to register the function for our shortcode and add it to the setup process to display random posts in wordpress.

[code]
// Creating the Shortcodes
function register_shortcodes(){
add_shortcode(‘random-posts', ‘random_posts_function');
}

// Registering the Shortcodes
add_action( ‘init', ‘register_shortcodes');
[/code]

Conclusion

Adding the utility to display random posts in WordPress only takes a few minutes of your time but it adds a lot to the variance and dynamics to your website. It helps you retain your viewers, and present them an option to choose from a variety of content whenever they visit your website. Follow the simple steps above to make it all happen. 😉

That's all for now:

If you've read all the way through this article, we are thankful. We have a large collection of articles, guides, and comparison reviews of eCommerce solutions, web hosting providers, website builders, and more! Feel free to check them out;

Please share any comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2023 All rights reserved