WordPress Random Posts Guide

Learn how to add & display random posts into your WordPress sidebar using easy methods and without codes knowledge required at this guide

By Claudio Pires
Updated on October 27, 2022
WordPress Random Posts Guide

Learn how to add and display random posts to your WordPress sidebar using easy methods and without the code knowledge required in this guide.

Displaying random WordPress 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.

WordPress doesn’t come with any built-in feature to show random posts. Luckily, there are some workarounds to show random WordPress posts on 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. This is called reverse chronology. WordPress uses reverse chronology to ensure that your homepage or 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 positions on the sidebar. Compared with internal linking, this will show the articles’ full title and other meta information. This makes it more likely for the visitors to be interested in these posts.

  • Equally, distribute access to all positions.
  • Increase your post’s chances of ranking on Google.
  • Make your website more generic.

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

Show Randomized WordPress Posts on the Homepage

With the PostX plugin, you can easily display random posts on your custom homepage, archive page, or sidebar. You can download the PostX plugin from the WordPress plugin directory. There are free and pro versions.

In the free version, you will get many basic Guttenberg editor solutions. The Pro version of PostX is also cheap and easy to use. It has many advanced features for creating professional blogging sites.

Well, let’s see how to display random posts on a custom page:

  1. Go to the side section.
  2. Click Add New/You can select an existing page.
  3. Click the plus sign (+).
  4. Select the desired PostX block.
  5. Go to Settings in the right sidebar.
  6. Click Query.
  7. You will now see a source option. Select posts here.
  8. Then select the “Random Posts” option in the “Quick Lookup” option.

Displaying Randomized Posts With a Plugin

Several WordPress plugins allow you to show random WordPress posts. I especially like the Advanced Random Posts Widget plugin among these plugins. The plugin makes it very simple to display the 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 on the Appearance -> Widgets page.

#1. Displaying Random Posts With a Plugin
WordPress widgets

Drag the widget to your desired location. You will find several configuration options. These enable you to define the title, and 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 options include displaying the thumbnail, defining the width, height, thumbnail alignment, showing the excerpt, date, etc. So, there is a separate section for providing Custom CSS too.

WordPress Random Posts Guide
WordPress Random-Posts Guide

Manually Displaying Random-Posts

In this method, you must add some code to the functions.php file of your theme. You can access the file using the file manager provided by your hosting provider, an FTP client, or a 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 fewer posts, change the value of ‘posts_per_page‘ in the code.

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.

Claudio Pires

Claudio Pires is the co-founder of Visualmodo, a renowned company in web development and design. With over 15 years of experience, Claudio has honed his skills in content creation, web development support, and senior web designer. A trilingual expert fluent in English, Portuguese, and Spanish, he brings a global perspective to his work. Beyond his professional endeavors, Claudio is an active YouTuber, sharing his insights and expertise with a broader audience. Based in Brazil, Claudio continues to push the boundaries of web design and digital content, making him a pivotal figure in the industry.