Showing Random Posts In WordPress
Showing Random Posts in WordPress
Displaying random posts in WordPress is a popular way to increase user engagement by showcasing diverse content each time a visitor lands on your site. Whether you want to highlight different blog posts, products, or portfolio items, showing random posts keeps your site dynamic and encourages visitors to explore more.
Quick Fix: Show Random Posts in WordPress
- Use the
WP_Query
class with'orderby' => 'rand'
to fetch random posts. - Add the code snippet to your theme’s
functions.php
or create a small plugin. - Call the function in your template files or use a shortcode to display random posts anywhere.
Why This Happens
WordPress does not provide a built-in widget or block to show random posts by default. You need to customize the query to order posts randomly. Using 'orderby' => 'rand'
in WP_Query
instructs WordPress to shuffle the posts before returning them. This approach is simple and effective but requires adding custom PHP code or using a plugin.
When to Use Showing Random Posts
- Increase page views: Encourage visitors to browse more content.
- Highlight older posts: Give visibility to posts that might otherwise be overlooked.
- Enhance user experience: Make your site feel fresh and dynamic on each visit.
- Promote diverse content: Showcase different categories or post types randomly.
Updated Code for Modern WordPress
Here is a clean, modern, and reusable function to fetch and display random posts. It uses WP_Query
with proper escaping and supports customization of post type and number of posts.
function show_random_posts( $args = array() ) {
$defaults = array(
'post_type' => 'post',
'posts_per_page' => 5,
'orderby' => 'rand',
'post_status' => 'publish',
);
$query_args = wp_parse_args( $args, $defaults );
$random_query = new WP_Query( $query_args );
if ( $random_query->have_posts() ) {
echo '<ul class="random-posts-list">';
while ( $random_query->have_posts() ) {
$random_query->the_post();
echo '<li><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p>No posts found.</p>';
}
}
How to Add This via functions.php
or a Small Plugin
Option 1: Add to functions.php
- Open your active theme folder.
- Locate and edit the
functions.php
file. - Paste the
show_random_posts()
function code at the end. - Call
show_random_posts();
in any template file where you want to display random posts.
Option 2: Create a Small Plugin
- Create a new folder named
random-posts-display
inwp-content/plugins
. - Create a file
random-posts-display.php
inside that folder. - Paste the following code:
<?php
/**
* Plugin Name: Random Posts Display
* Description: Display random posts anywhere using a shortcode.
* Version: 1.0
* Author: Your Name
*/
function rpd_show_random_posts( $atts ) {
$atts = shortcode_atts( array(
'post_type' => 'post',
'posts_per_page' => 5,
), $atts, 'random_posts' );
$query_args = array(
'post_type' => sanitize_text_field( $atts['post_type'] ),
'posts_per_page' => intval( $atts['posts_per_page'] ),
'orderby' => 'rand',
'post_status' => 'publish',
);
$random_query = new WP_Query( $query_args );
if ( $random_query->have_posts() ) {
$output = '<ul class="random-posts-list">';
while ( $random_query->have_posts() ) {
$random_query->the_post();
$output .= '<li><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>';
}
$output .= '</ul>';
wp_reset_postdata();
} else {
$output = '<p>No posts found.</p>';
}
return $output;
}
add_shortcode( 'random_posts', 'rpd_show_random_posts' );
- Activate the plugin via the WordPress admin dashboard.
- Use the shortcode
[random_posts]
in posts, pages, or widgets to display random posts.
Step-by-Step Test
- Add the function or plugin code as described above.
- Insert
<?php show_random_posts(); ?>
in a theme template file (e.g.,sidebar.php
orfooter.php
). - Or add the shortcode
[random_posts]
in the WordPress editor. - Visit your site’s front end and refresh the page multiple times.
- Verify that different posts appear randomly each time.
Block Themes & Gutenberg Notes
For block themes or full site editing (FSE), you cannot directly insert PHP code in blocks. Instead, use the shortcode method via a Shortcode block or create a custom block plugin that wraps this functionality.
- Insert a Shortcode block and add
[random_posts]
. - For advanced users, create a dynamic block that calls
show_random_posts()
on render. - Remember that caching plugins or server-side caching might prevent the randomization from updating on