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

How to Display Random Posts in WordPress

Last updated on by
Follow WPBeginner on YouTube
How to Display Random Posts in WordPress

Due to the natural structure of blogs, articles end up being buried down once they get old. Most new users join and pick up from the moment they join, and they never bother going back to one of your good posts. One of the easiest way to give a new life to your older posts is by displaying them in the sidebar. In this tutorial, we will share how you can display random posts in your WordPress.

All you have to do is open your sidebar.php, or any other file you like to add it in and paste the following code:

<li><h2>Random Post</h2>
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php } ?>
</ul>
</li>

You can customize the styling around it. If you want to display more than 5 posts, simply change the number 5 to the number of your choice.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Andre Stuart

    Just what I needed. Adding the code in the home page displays random posts but it stops the latest posts from appearing. Can you tell me what changes I have to make to this code so that I can have 3 random posts followed by the latest posts? Thanks in advance!

  • Easy

    Indeed you guys are doing a great job here. But this is not actually what am looking for. I am looking for how to display a random user from my blog. That is, a code that will select a random user from all the people that registered to my blog. Actually what I want to display is his or her email. I will appreciate any help. Thanks once again…

  • Wally

    Thanks for this kinda exactly what I’m looking for and stumbled upon.

    I was wondering if it’s possible to add thumbnails (extracted from images in a post) to this? How would that work?

    • http://www.wpbeginner.com/ WPBeginner Support

      Haven’t tested it but this should work:

      <li><h2>Random Post</h2>
      <ul>
      <?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
      <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?><?php the_title(); ?></a>
      </li>
      <?php } ?>
      </ul>
      </li>
      
      
      • Wally

        Thanksa lot! That worked I did try something similar but there was something wrong with my code.

        It displays the text right next to the image, but I want the title to display under the thumbnail. Now I did style it a little with giving the thumbnail a certain size and with adding display: inline but I’m stuck as what to do next to get that title under the thumbnail. Can you give me a hint in the right direction?

      • Wally

        Hi

        I was wondering if you could still help me out as I’m really stuck. I want to display the random posts horizontally. It works by removing the and tags but then I get the title next to the thumbnail (on the right). Like this:
        http://f.cl.ly/items/330h1F010X2y0y412Z0j/horizon.jpg

        But I want it to display like this example:
        http://f.cl.ly/items/0I1624113D3f0W3M2s3u/horizontal.jpg

        I’m wondering if you could help me out as I tried styling it but I think I’m doing something wrong and I really can’t figure out how to get that right. It would help out greatly and I guess it would also help other people who want this.

  • Animesh Roy

    I have read the post but I am using Genesis Child Theme in my blog. There is one function.php file, so can you suggest me a way how I can add Random Posts in my blog’s sidebar.

    If I use text widget to add random post in the sidebar then how can I do that.

    Please suggest me the proper way.

  • Jan Willem

    Hi great article!

    One thing, i’ve added this code above the page, but now my pages don’t show up anymore… just my testpost on every page….

    I’m using twentyeleven.

    Hope you guys can help me out.

    • http://www.wpbeginner.com Editorial Staff

      Try adding this below the code:

      wp_reset_postdata();?

  • Brian

    Just added this to the bottom of a post and the comments have changed to the comments for the last random post entry. How can this be setup so you can put a random post section between content and comments but the comments are the actual main post / article comments? Thanks!

    • http://www.wpbeginner.com Editorial Staff

      Change line 6 to be like this:

      <?php } wp_reset_postdata(); ?>
  • Cristian B.

    This can be implemented for the normal pagination of the blog posts? I just want to see that in a random order. Also, If I have more than one page of that random posts (10 per page) I must don’t see posts in the second page that are present on the first.

    Are here a possibility to do that?

    • http://www.wpbeginner.com Editorial Staff

      No. If your order is random, then it defeats the purpose of pagination.

  • nurandi

    How to display summary for each random post title?

    • http://www.wpbeginner.com Editorial Staff

      Just add the following code after the_title code.

      <?php the_excerpt(); ?>
      
      • Phil Hart

        Hi, I used this technique for displaying a random blog post on my home page, and the function pulls through content from my home page rather than the excerpt from the blog.

        Have you got any clever tricks to fix this? :)

        Thanks in advance,
        Phil

  • Katie Shades

    Bang on Target, thanks for this article. Rather than rand you can also use offset to show different posts.

  • vaibhav

    How can i display the post in horizontal manner

    • vaibhav

      FIX
      Just remove the and tags from the code

  • http://www.infogenra.com/ rajkanuri

    I have used the same code but it is displaying same posts for 5 times.. what could be the issue..

    • wpbeginner

      @rajkanuri The issue is within your theme. Usually when a query is not resetted properly, this will follow that query. It is mostly caused by recent posts widget, but its not limited to that.

  • wpbeginner

    @mindrise If you use the loop and then query_post, you should be able to do this.

  • http://www.mindrisehypnosis.com/ mindrise

    how can I show only a list of posts that came before the current post your on. example:

    if you post #5 the sidebar list posts 1-4 sam as if you go to post 9 side bar will list posts 1-8.

    any idea?

  • Lisa

    How would you add the parameter to display posts only in a specific category as well as randomly and 5 at a time?

    • http://www.wpbeginner.com Editorial Staff

      You can add cat=categoryid << and then keep the other code the same.

  • http://kb9.org Kaushik Biswas

    I am considering doing this same thing, but not in sidebar, I wish to place it right after the current post ends.

    • http://www.wpbeginner.com Editorial Staff

      Good idea :)

  • http://www.egracecreative.com Brandon Cox

    By the way, instead of displaying the_title, you can just put “Read a Random Post” and limit it to show only one. Create a nice button and you’ve got a “read a random post button” like you see on some special navigation bars sometimes.

  • http://www.joyoge.com joyoge bookmark

    helpful post, thanks a lot..