After running WordPress sites for over 15 years, I‘ve seen first-hand how showing random posts can increase user engagement. By showcasing varied content, you expose visitors to older posts they may have missed.
Displaying a random sampling of articles helps add unpredictability and encourages exploration. One study found pages with random content recommendations had 68% higher click-through rates.
In this beginner‘s guide, we‘ll look at two straightforward ways to display random posts in WordPress:
Contents
Option 1: Use a Dedicated Plugin
For bloggers just getting started, a dedicated random post plugin is the simplest approach. It handles all the complex code behind the scenes for you.
I recommend the Random Post Widget plugin. It‘s free, well-supported, and easy to set up.
Step-by-Step Installation
-
Install and activate the Random Post Widget plugin from your WordPress dashboard.
-
Go to Appearance > Widgets.
-
Find the "Random Post Widget" and drag it into a widget area like your sidebar.
-
Configure the widget settings:
-
Title – The headline for your widget.
-
Number of posts – How many random posts to display.
-
Post types – Which content types to randomize (posts, pages, etc).
-
-
Click Save and view your site to see the widget in action!
The plugin will track recently shown posts so the same posts won‘t keep re-appearing on each page load.
The free version includes the core functionality while Random Post Widget Pro ($29) adds options for custom post types, taxonomies, exclusions, and more.
Why Use a Plugin?
Dedicated plugins like Random Post Widget make adding random posts simple for beginners. The benefits include:
-
Works out of the box – No coding needed. Just install, configure widget, done.
-
Actively maintained – Plugins are kept updated and compatible with new WordPress versions.
-
Good for beginners – Great way to add dynamic content if you‘re not coding yet.
-
Easy to reuse – Can add the widget anywhere with a few clicks.
The plugin approach gets you up and running quickly. But advanced users may want more customization, which brings us to…
Option 2: Use Custom Code
For full control over output, you can display random posts by adding code to your theme files. This allows endless customization but requires coding knowledge.
There are two code-based methods:
1. Display in a Specific Location
To show random posts in a set location like your sidebar, add PHP code directly in the matching template file:
// Inside sidebar.php
<?php
$posts = get_posts(array(
‘post_type‘ => ‘post‘,
‘orderby‘ => ‘rand‘,
‘posts_per_page‘ => 5
));
if($posts) {
echo ‘<h3>Random Articles</h3><ul>‘;
foreach($posts as $post) {
// Setup post data
setup_postdata( $post );
// Output linked post title
echo ‘<li><a href="‘ . get_permalink() . ‘">‘ . get_the_title() . ‘</a></li>‘;
}
echo ‘</ul>‘;
// Reset query
wp_reset_postdata();
}
?>
This queries WordPress for 5 random posts, then displays the titles linked to the post URL.
Adjust the parameters like post type or number of posts as needed. Just be sure to reset the query at the end.
2. Use a Shortcode
For maximum flexibility, you can create a shortcode to display random posts anywhere:
// In functions.php or a plugin
function display_random_posts_shortcode( $atts ) {
// Default attributes
$atts = shortcode_atts( array(
‘num_posts‘ => 3,
‘post_type‘ => ‘post‘
), $atts, ‘random-posts‘ );
// Query random posts
$posts = get_posts(array(
‘post_type‘ => $atts[‘post_type‘],
‘orderby‘ => ‘rand‘,
‘posts_per_page‘ => $atts[‘num_posts‘]
));
// Output posts in a list
if($posts) {
$html = ‘<ul>‘;
foreach($posts as $post) {
setup_postdata( $post );
$html .= ‘<li>‘;
$html .= ‘<a href="‘ . get_permalink() . ‘">‘;
$html .= get_the_title();
$html .= ‘</a>‘;
$html .= ‘</li>‘;
}
$html .= ‘</ul>‘;
wp_reset_postdata();
} else {
$html = ‘No posts found‘;
}
return $html;
}
add_shortcode( ‘random-posts‘, ‘display_random_posts_shortcode‘ );
You can then add the shortcode anywhere:
[random-posts]
It also accepts attributes to customize the output:
[random-posts num_posts=3 post_type="pages"]
Shortcodes provide more flexibility to display random posts where you want.
Comparing Code Solutions
Coding your own random post output gives unlimited options but requires PHP knowledge. Here‘s how the two code approaches compare:
Hardcoded | Shortcode | |
---|---|---|
Good For | Set locations like sidebars | Anywhere – reusable |
Customization | Need to edit template files | Easily change shortcode parameters |
Coding Required | PHP | PHP + HTML basics |
Hardcoding is good for fixed widget areas. Shortcodes offer more flexibility.
Both take more work than plugins but provide granular control over random posts.
Final Thoughts
-
Showing random posts keeps your content fresh and promotes discovery.
-
For beginners, start with an easy plugin like Random Post Widget to add a random post widget.
-
For advanced customization, use PHP code to output random posts anywhere.
-
Try different placements and amounts of random posts to find the right mix.
Let me know if you have any other questions! I‘m always happy to help fellow WordPressers implement random posts effectively.