Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Showing Random Posts In WordPress

Posted on August 19, 2025 By Admin No Comments on 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

  1. Use the WP_Query class with 'orderby' => 'rand' to fetch random posts.
  2. Add the code snippet to your theme’s functions.php or create a small plugin.
  3. 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

  1. Open your active theme folder.
  2. Locate and edit the functions.php file.
  3. Paste the show_random_posts() function code at the end.
  4. Call show_random_posts(); in any template file where you want to display random posts.

Option 2: Create a Small Plugin

  1. Create a new folder named random-posts-display in wp-content/plugins.
  2. Create a file random-posts-display.php inside that folder.
  3. 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' );
  1. Activate the plugin via the WordPress admin dashboard.
  2. Use the shortcode [random_posts] in posts, pages, or widgets to display random posts.

Step-by-Step Test

  1. Add the function or plugin code as described above.
  2. Insert <?php show_random_posts(); ?> in a theme template file (e.g., sidebar.php or footer.php).
  3. Or add the shortcode [random_posts] in the WordPress editor.
  4. Visit your site’s front end and refresh the page multiple times.
  5. 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
WordPress Snippets Tags:Loop, PHP, Query, Theme, WordPress

Post navigation

Previous Post: Showing Amount Of Comments A Comment Author Made
Next Post: Adding A Custom Field Automatically On Post/Page Publish

Leave a Reply Cancel reply

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

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com