WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Display Random Posts in WordPress

How to Display Random Posts in WordPress

Last updated on August 13th, 2018 by Editorial Staff
194 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
How to Display Random Posts in WordPress

Do you want to display random posts in WordPress? Displaying random posts gives your users a chance to discover more of your content. In this article, we will show you how to easily display random posts in WordPress.

How to display random posts in WordPress

Why and Where to Display Random Posts in WordPress

By default WordPress lists your blog posts in reverse chronological order (from newest to oldest). This allows users to see your latest posts first.

However, most users will not get to see your older articles. If you have been running your site for quite some time now, then your older articles will not be prominently displayed anywhere.

One way to overcome this is by making internal linking a habit. Linking to your older articles in new posts will help users discover them. It will also increase your pageviews and improve your SEO score.

Another way around that is by displaying random posts in your sidebar. This way your users will get to discover posts that they would not see otherwise.

Having said that, let’s see how you can easily display random posts in WordPress.

Video Tutorial

Subscribe to WPBeginner

If you don’t want to watch the video tutorial, then you can continue reading the text version below:

Method 1: Display Random Posts in WordPress with a Plugin

This method is easier and is recommended for most users.

First thing you need to do is install and activate the Advanced Random Posts Widget plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Appearance » Widgets page. You will notice a new widget labeled ‘Random Posts’ under the list of available widget.

You need to add this widget to a sidebar. See our guide on how to add and use widgets in WordPress.

Now, you will be able to see the widget settings. The default options will work for most websites, you can just click on the save button.

Random posts widget settings

Advanced Random Posts Widget is a powerful plugin with tons of customization options. You can select different post types, show excerpt, show thumbnail, and skip posts you don’t want to show, or displays posts from specific categories or tags.

For more experienced users, the plugin also allows you to add custom before and after HTML, and your own custom CSS as well.

Don’t forget to click on the save button to store your widget settings. You can now visit your website to see random posts widget in action.

Random posts with thumbnail and excerpt

Method 2: Display Random Posts in WordPress Using Code

This method requires you to add code to your WordPress theme files. See our guide on how to copy paste code in WordPress.

First thing you need to do is add this code in your theme’s functions.php file or a site-specific plugin.

function wpb_rand_posts() { 

$args = array(
	'post_type' => 'post',
	'orderby'	=> 'rand',
	'posts_per_page' => 5, 
	);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

$string .= '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		$string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
	}
	$string .= '</ul>';
	/* Restore original Post Data */
	wp_reset_postdata();
} else {

$string .= 'no posts found';
}

return $string; 
} 

add_shortcode('wpb-random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode'); 

This code simply creates a function that displays 5 random posts. It then creates a shortcode so that you can easily display random posts anywhere on your site. Lastly, it enables shortcodes to be executed inside WordPress widgets so that you can use shortcode inside a text widget.

Now you can display random posts inside a WordPress post, page, or text widget using the shortcode [wpb-random-posts].

Plain random posts list

That’s all, we hope this article helped you learn how to display random posts in WordPress. You may also want to see these 12 WordPress sidebar tricks to increase pageviews.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

194 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

29 Comments

Leave a Reply
  1. Michael says:
    Jan 14, 2021 at 3:58 am

    Is there an easy way to cache the results for a certain period of time instead of randomizing every time? Maybe like refresh once every 24hrs to reduce lessen the load on DB calls every time?

    Reply
    • WPBeginner Support says:
      Jan 14, 2021 at 9:58 am

      For that functionality, you would want to check with the plugin’s support for if it is available.

      Reply
  2. Marvin Duru says:
    Jul 21, 2020 at 1:56 pm

    Please, i tried adding the shortcode on a specific position on my website not inside the post in PHP but the Random post section did not display but only show my text “[wpb-random-posts]”.

    What should i do?

    Reply
    • WPBeginner Support says:
      Jul 23, 2020 at 9:51 am

      When using the block editor you would want to ensure you’re adding it using a shortcode block.

      Reply
  3. Zeynep says:
    Aug 31, 2019 at 9:38 am

    Thanks the code works just fine. I changed the first page in my wordpress site to index.html, what do I need to run [wpb-random-posts] on my html page? My English is a bit weak, I hope you understand me.

    Reply
    • WPBeginner Support says:
      Sep 4, 2019 at 9:27 am

      You would need the page to be a WordPress page for a WordPress shortcode to work.

      Reply
  4. Elle says:
    Aug 29, 2019 at 3:28 pm

    How would you go about styling this? I’m getting confused as to how I would control the design of this, i’ve never worked with styling PHP.

    Reply
    • WPBeginner Support says:
      Aug 30, 2019 at 9:21 am

      It would depend on the styling you’re wanting to add for what needs to be done. You can add a class in the html code to give you something to target.

      Reply
  5. Maria Cristhiane Amorim says:
    Aug 4, 2019 at 12:11 pm

    Hello! Can I do this in a listing?

    Reply
    • WPBeginner Support says:
      Aug 5, 2019 at 11:44 am

      You would need to clarify what you mean by that

      Reply
  6. Rob Packenham says:
    Jan 6, 2018 at 5:46 pm

    Thanks for the helpful article.

    I have a question — my WordPress site pages include a sidebar with a random post (excerpts) widget.

    A problem is that Google indexes the pages and ‘sees’ the random post excerpts as content for the page. So in Google search, a page can show up as matching a search term only because one of the random post excerpts on the page matched the term (not the main content of page).

    But, when a user clicks on the link in Google search results they will see the page with a different set of random post excerpts in the sidebar — not the post excerpt with matching text. So it looks to them like a bad search.

    Ideally i’d like to exclude the random post excerpts from Google index ie hide them from Google, but that doesn’t look possible (?).

    If you have any suggestions I’d appreciate it! Thanks.

    Reply
  7. Jun Enriquez says:
    Sep 25, 2017 at 2:33 am

    Tried this & it destroyed my site, how do I reset this?
    http Error 500
    Please help
    Thanks

    Jun

    Reply
  8. Yin says:
    Sep 3, 2017 at 11:02 pm

    Thanks a million for sharing this. I used another plugin for random post that no longer works. Then I find your site and have installed the plugin. It works like a charm.

    Reply
  9. Lisa says:
    Jul 29, 2017 at 5:22 am

    Hi, and thanks for this helpful tutorial!
    Can you tell me what would be the piece of code to add to display random posts that are in a specific category? My theme doesn’t display a sidebar so I can’t use the plugin and it already has a random posts display under each article, but posts from all categories are included and displayed then. I tried to go check the theme code and add ‘post_category’ => ‘name-of-the-category’, but it doesn’t work. Any idea?

    Reply
  10. Raff says:
    Jul 19, 2017 at 10:34 pm

    Hi! I just want to ask what if i want to change random post every week or any given time. For example, i display a set of random post, it will not change until 7days or any given time. Thanks!

    Reply
  11. Abhimanyu Kashyap says:
    Apr 30, 2017 at 8:35 am

    Thanks a ton, I was able to display random posts. I changed the no of posts to 1 to display 1 random post. Is there a better way to do it?
    I would also like to find out how I can use a variable instead of post type and enter one where I place the short code eg – [wpb-random-posts(‘post’)].
    How can I achieve this ??

    Reply
  12. devender says:
    Apr 19, 2017 at 4:29 am

    thanks shearing this code its very helpful……..

    Reply
  13. Nate says:
    Apr 11, 2017 at 9:19 pm

    Setting posts to orderly => ‘rand’ is a half-measure. Sure, the post displays ‘randomly’, but like DI SEKITAR mentioned above, what use is displaying one random post if it’s going to be the same random post every time someone visits the site?

    Reply
    • Nate says:
      Apr 11, 2017 at 9:20 pm

      Meant to say ‘orderby’ … I was spellcheck’d

      Reply
  14. smehsan says:
    Mar 6, 2017 at 8:29 am

    Sorry, But the string was not working and i just make the string global and it works fine now.

    global $string;

    Reply
  15. Marléne Rose Shaw says:
    Feb 26, 2017 at 3:33 am

    Thanks for this great tutorial. Is there some way I can get the shortcode and then display the random posts beneath the content in a page instead of the sidebar??

    Reply
  16. Katherine says:
    Feb 4, 2017 at 12:08 pm

    JAYKAY

    To exclude the current post add

    ‘post__not_in’ => array(get_the_id())

    to the query.

    So your args will now be:

    $args = array(
    ‘post_type’ => ‘post’,
    ‘orderby’ => ‘rand’,
    ‘posts_per_page’ => 5,
    ‘post__not_in’ => array(get_the_id()),
    );

    Reply
    • JAYKAY says:
      Feb 8, 2017 at 7:16 am

      It works fine with this piece of code.

      Thank you KATHERINE

      Reply
  17. di sekitar says:
    Feb 2, 2017 at 2:32 am

    really cool information,
    I tried used plugin that you mention, but i have big question.

    How to make random post different for every visitor or device that visiting my site?

    because i try visit my site using other notebook and smartphone, but the random post from those 2 device is same.

    Reply
  18. JayKay says:
    Jan 25, 2017 at 5:06 am

    Hi,

    Advanced Random Posts Widget is a really good plugin, but i have a problem.
    How could you exclude the current/active post when you display a random post ?
    Is it possible to fix this ?

    The problem is the same with the method 2 code.

    Reply
    • Harry Stoddart says:
      Jul 3, 2017 at 7:02 pm

      Use the “Offset” variable to skip the latest X posts.

      Reply
  19. Khalil Sillah says:
    Jan 14, 2017 at 8:04 pm

    As a beginner what list of matrial would I need to learn wordpress

    Reply
    • Ahm Clex says:
      Jun 22, 2017 at 1:10 pm

      A PC, WiFi and dedication. That’s all you need to learn anything this days.

      Reply
  20. Gaurav Khurana says:
    Jan 12, 2017 at 9:52 pm

    thanks for this. i was always thinking about this, Since by default the latest post are visible on the home page itself. Thanks for this will definetely use this plugin

    Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WPForms Logo
WPForms
Drag & Drop WordPress Form Builder Plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2021 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2021)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2021)
    • SiteGround Reviews from 4464 Users & Our Experts (2021)
    • Bluehost Review from Real Users + Performance Stats (2021)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2021 – Step by Step Guide
Deals & Coupons (view all)
Cozmoslabs
Cozmoslabs Coupon
Get 15% OFF on Cozmoslabs WordPress premium plugins.
CMS Commander
CMS Commander Coupon
Get 20% OFF on CMS Commander plugin for website management.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
  • Growth Fund
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon
  • AIOSEO

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress Security by Sucuri.