How To Display Random Posts In WordPress Without Plugin

How To Display Random Posts In WordPress Without Plugin

So you want to display random posts for your WordPress powered website? This tutorial will show you 5 different methods to display random posts in WordPress.

Why You Should Display Random Posts In WordPress

There are many benefits of display random posts in WordPress. By default, WordPress displays the posts in reverse chronological order (from newest to oldest). This allows users to see the latest posts first, but it also made it hard for them to discover older contents. By display random posts from your WordPress site, you are making it easier for users to find older posts. This will increase your site’s pageviews and improve its SEO score as well.

Where Should You Display Random Posts In WordPress

One of the best places to display the random posts is below the post content. You can create a You May Also Like section and display the random posts there. Another popular option is in the sidebar with heading similar to Popular Posts or What Others are reading now. You can also choose to display the random posts in the footer, but I highly advise not to.

Enough talk, let’s go into the actual code.

How To Show Random Posts On Homepage For WordPress

Copy and paste the below code in any part of your theme to display random posts.


<ol>
    <?php
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'post__not_in' => array(get_the_id()), //excluding the current 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();
                $rand_posts .= ''. get_the_title() .'';
            }
            echo $rand_posts;
            wp_reset_postdata();
        } 
    ?>
</ol>

The above code is ideal for use on homepage, category, tag and archive pages. It will randomly display 5 published post from your site. You can change the number of posts being display by changing ‘posts_per_page’ => 5 to the number you desired.

How To Display Random Posts On single.php


<ol>
    <?php
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'post__not_in' => array(get_the_id()), //excluding the current 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();
                $rand_posts .= ''. get_the_title() .'';
            }
            echo $rand_posts;
            wp_reset_postdata();
        } 
    ?>
</ol>

Above code will randomly display 5 published posts excluding the current post.

How To Display Random Posts From a Category


<ol>
    <?php
    $args = array(
        'tag' => 'html-css', //slug of the tag
        'post_type' => 'post',
        'post_status' => 'publish',
        '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();
            $rand_posts .= ''. get_the_title() .'';
        }
        echo $rand_posts;
        wp_reset_postdata();
    } ?>
</ol>

Above code will randomly display 5 published posts from category with the ID=1. You can change 'cat' => 1 to any category ID you wish to get the random posts from.

How To Display Random Posts From a Tag


&lt;ol&gt;
    &lt;?php
    $args = array(
        'tag' => 'html-css', //slug of the tag
        'post_type' => 'post',
        'post_status' => 'publish',
        '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();
            $rand_posts .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
        }
        echo $rand_posts;
        wp_reset_postdata();
    } ?>
&lt;/ol&gt;

Above code will randomly display 5 published posts from tag with slug=html-css. You can change 'tag' => 'html-css' to any tag slug you wish to get the random posts from.

How To Display Random Posts From Custom Post Type


<ol>
    <?php
    $args = array(
        'post_type' => 'tutorial', //name of the Custom Post Type
        'post_status' => 'publish',
        '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();
            $rand_posts .= ''. get_the_title() .'';
        }
        echo $rand_posts;
        wp_reset_postdata();
    } ?>
</ol>

Above code will randomly display 5 published posts from tutorial post type. You can change 'post_type' => 'tutorial' to any post type you wish to get the random posts from.

We hope this tutorial helped you learn how to display random posts in WordPress. If you are not comfortable with editing theme files, please see our tutorial on How to display random post in WordPress with plugin.

You May Also Like