How to Display Random Posts in WordPress to Increase Engagement

display-random-posts-in-wordpress

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.

Read Also: Hidden WordPress Features you can start using from today!

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. Alternatively, you can move to hosting providers like WPEngine and Bluehost to analyze which option is best for you.

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

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

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:-

random-posts posts=5 cat=themes,plugins]

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.

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

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.

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

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

<?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>





';

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.

// resetting the WordPress query
wp_reset_query();

// displaying the list of random posts
return $return_string;

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

// Creting the Shortcodes
function register_shortcodes(){
add_shortcode('random-posts', 'random_posts_function');
}

// Registering the Shortcodes
add_action( 'init', 'register_shortcodes');

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. 😉