🔥 Holiday Sale! 25% Off Platinum Membership and up to 50% Off on Deployarr (ends December 31).

How to display a random recent post in WordPress?

This post explains how to get and display one random recent post in WordPress. WordPress can be even more powerful than it already is if you know PHP and are capable of customizing some of the scripts that power WordPress blogging platform. Unfortunately most bloggers are not coders and therefore are not comfortable coding. I probably fall somewhere in between and so when I find something that worked for me, I try to share it with others (like this one). Anyway, displaying recent posts in WordPress is easy using the using the built-in wp_get_recent_posts function. However, in my case I wanted to display just one recent random post from a list of say 10 or 15 most recent posts in my Genesis Lifestyle Theme. There is no function to do this at this point. However, with just a little bit of improvisation to the wp_get_recent_posts we can display a random recent post in WordPress.

What I wanted to Achieve

You may have noticed the deals notification bar that appears on top of the page. Below is a screenshot of it.

Random Recent Post Wordpress
Deals Notification Bar

I have a custom post type called "Deals" in which I aggregate recent deals in electronics and display it through the top notification bar. On clicking "Get it" it takes the visitor to the deal page. On clicking "See All Deals" the visitor is sent to the deals of the day page that list all currently active electronics deals.

Electronics Deals
Electronics Deals

Previously, I had it so that a random post was displayed from all available electronics deals (using the wp_get_recent_posts function). However, some of the deals expire and these old entries still showed up in the notification bar. So I had to find a way to show just one random recent post from the 10 or 15 most recent deals. The assumption here is that the recent 10 to 15 deals, because they are recent (1 to 2 days old), are active deals.

Displaying Random Recent Post

If you are not aware of the wp_get_recent_posts it might help if you review it before proceeding.

Below is the code to display one recent random post in WordPress. In my case, it is a recent deal instead of a post.

Code To Add One Recent Random Post
Code To Add One Recent Random Post (Click Here For Text Code)

The $rdargs variable sets the arguments to be passed to wp_get_recent_posts function. In this case, we get 15 most recent deals that have been published. If you want a random recent post then change deal to post in post_type. You may also change 15 to whatever number you wish. A full list of available arguments are available here.

Then, get all of those recent deals to the array $recent_deals. Now here is the trick, the shuffle function randomly shuffles the array contents. So we shuffle the 15 most recent deals contained in the variable $recent_deals.

Finally, we create a foreach loop to go through the array contents one by one and display them. However, in this case we want only one random recent post. The break command takes care of this. After the first recent deal in $recent_deals is displayed the loop ends. In my case, this achieved what I wanted to achieve, which is displaying a single random deal from the 15 recent deals in the top notification bar.

While these situations are rare sometimes we do have to find a way to achieve what we want. So there you go, just a simple PHP code to display a random recent post in WordPress. If you have other ways in which you achieved this please share them with us in the comments section.

Be the 1 in 200,000. Help us sustain what we do.
125 / 150 by Dec 31, 2024
You will gain benefits such as Deployarr access, discord roles, exclusive content, ad-free browsing, and more.
🔥 Holiday Sale! 25% Off Platinum Membership $399.99 $299.99 (ends December 31).
Join the Geek Army (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.

Holiday Sale