How to List Random Posts in WordPress: A Comprehensive Guide

Home » Snippets » How to List Random Posts in WordPress: A Comprehensive Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you searching for a creative way to keep your website visitors engaged and intrigued? Displaying random posts on your WordPress website could be the perfect solution. Random posts can help to expose readers to content they may have otherwise not found. In this article, we will explain how to list random posts on your website, offering an exciting and unpredictable experience for your users.

					function wpturbo_random_posts() {
    $args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'posts_per_page' => 5,
    );
 
    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            the_title();
            echo '<br>';
        }
        wp_reset_postdata();
    } else {
        echo 'No posts found';
    }
}
add_shortcode( 'random-posts', 'wpturbo_random_posts' );
				

The first line of the code defines a function named wpturbo_random_posts(). This function handles the generation of a list of random posts and it’s what we will be using throughout the entire process.

function wpturbo_random_posts() {

Inside this function, an $args array is established. This array defines the parameters for the posts we’ll be querying.

  • 'post_type' => 'post', is specifying that we want entries labeled as posts. The ‘post’ variable can be replaced by any registered custom post type.
  • 'orderby' => 'rand', tells WordPress that we want these posts to be ordered randomly.
  • 'posts_per_page' => 5, means that we want to display five posts per page.
$args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'posts_per_page' => 5,
    );

The next line creates a new instance of WP_Query object by passing the $args array. WP_Query is a class in WordPress that deals with the retrieval of posts given certain parameters.

$the_query = new WP_Query( $args );

Now, a conditional check is introduced using if ($the_query->have_posts()). This check ensures that even if there are no posts available, WordPress won’t encounter an error and halt.

if ( $the_query->have_posts() ) {

If posts are available, WordPress enters a while loop and posts are output one by one using the_title() function. The break <br> after each title ensures each title is printed on a new line.

while ( $the_query->have_posts() ) {
            $the_query->the_post();
            the_title();
            echo '<br>';
        }

After displaying all the queried posts, WordPress restores the original query and post data with the wp_reset_postdata() function. This is important when using multiple loops on a page.

wp_reset_postdata();
        } else {

Should no posts be found, a message will be displayed on the page with the line ‘echo ‘No posts found’;’.

echo 'No posts found';
    }

The last line registers the wpturbo_random_posts function as a shortcode, accessible via the shortcode [random-posts]. This allows the function to be invoked anywhere shortcodes are processed within your theme or plugin.

add_shortcode( 'random-posts', 'wpturbo_random_posts' );

Put together, this code will list a specified number of posts in random order wherever the [random-posts] shortcode is inserted.

Register an account to save your snippets or go Pro to get more features.