How to Show a Random Post on the Homepage in WordPress

How to Show a Random Post on the Homepage in WordPress

By default, WordPress displays all the posts on the homepage in reverse chronological order. This works out great for sites with continuously changing content like a news or technology blog. However, this leads to what I feel is a huge “wastage” of content that can still be relevant. Not all content is time sensitive. An outdated news story certainly doesn’t need to be resurfaced. But an interesting political analysis might always be relevant.

We’ve talked about republishing older blog posts before, and that’s one way to get leverage out of your existing content. But what if you could randomly serve up blog posts on your homepage everytime a visitor viewed it? There are lots of plugins and pieces of code that show you random blog posts on the sidebar in the form of a widget. But so far, I haven’t seen anything that displays a random blog post on the homepage.

I’m not looking to republish older blog posts, or replace the existing reverse chronological set up. I just want to show a random post on the homepage in WordPress above all the others, so that users are exposed to older content. These random posts can be selected from a pool of existing categories, or something else if necessary. But how do we go about this?

Appropriating Sticky Posts

There’s already an existing mechanism in WordPress for showing older posts above the default homepage. They’re called “sticky posts”. Basically whenever you create or edit a post, you can click the “Visibility” checkbox on the right hand side widget as shown here:

This will designate that particular post as “sticky”. What this means is that this post will display above all the others on the home page. It’s usually used for semi-permanent notices for example, where an admin wants to draw the reader’s attention to something important.

However, it occurred to me, that I can appropriate this mechanism to display random posts on a homepage. This way, I won’t need to mess around with styling, or modify the query, or perform any other kind of expensive operation. Here’s how I went about it.

Code for Displaying a Random Post on the Homepage

First, here’s a screenshot of my test blog with the default latest post:

To display random posts on the WordPress homepage with sticky posts, I use the following steps:

  1. Get the ID of a random post – in a specific category if necessary
  2. Set it as the sole sticky post
  3. Display the homepage

Here’s the code to accomplish this:

 

function create_random_sticky() {

    if (!is_home()) {
            $stickies = array();
            $updated = update_option( 'sticky_posts', $stickies );
            return;
    }

    $args = array( 
        'orderby' => 'rand',
        'posts_per_page' => '1', 
    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
    $post_id = get_the_ID();
    endwhile;

    $stickies = array($post_id);
    $updated = update_option( 'sticky_posts', $stickies );

    
}
add_action( 'template_redirect', 'create_random_sticky' );

If you don’t know how to add code like this to WordPress, take a look at my earlier tutorial on using code snippets in functions.php or in a custom PHP plugin. With this code enabled, a new sticky post shows up each time the homepage is rendered:

Show a Random Post on the Homepage

A few things to note. First, this code erases all sticky posts each time it shows a page. So if you’re already using the sticky posts mechanism, it’s not for you. Second, it uses the entire pool of posts for finding random posts. If you want, you can easily modify this by specifying a category, or some other parameter like a custom post type in the “$args” parameter in the code above. Like this:

 

    $args = array( 
        'orderby' => 'rand',
        'posts_per_page' => '1', 
        'category_name' => 'some_category'
    );

Replace “some_category” above, with the name of the category from which you want to select random posts, and you’re done!

One final thing. If you decided to discontinue this code, there will be one sticky post still remaining. Find out which one it is by visiting your homepage, go to “Edit”, and then uncheck the box marking it as a sticky.

2 Comments on “How to Show a Random Post on the Homepage in WordPress”!

  1. Avatar

    Hello, is there anyway to increase Random Post, I am Going on Leave and I Want to Show More Random Post While I on Leave, Above Scripts Worked Fine, I Tried to change ‘posts_per_page’ => ‘1’, to 2,3,4, But Not Worked,

    Please Help Me,

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.