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
Elegant Themes
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 »

Comments

  1. Andre Stuart says:

    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!

  2. Easy says:

    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…

  3. Wally says:

    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?

    • WPBeginner Support says:

      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 says:

        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 says:

        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.

  4. Animesh Roy says:

    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.

  5. Jan Willem says:

    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.

  6. Brian says:

    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!

  7. Cristian B. says:

    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?

  8. nurandi says:

    How to display summary for each random post title?

    • Editorial Staff says:

      Just add the following code after the_title code.

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

        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

  9. Katie Shades says:

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

  10. vaibhav says:

    How can i display the post in horizontal manner

  11. rajkanuri says:

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

    • wpbeginner says:

      @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.

  12. wpbeginner says:

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

  13. mindrise says:

    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?

  14. Lisa says:

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

  15. Kaushik Biswas says:

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

  16. Brandon Cox says:

    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.

  17. joyoge bookmark says:

    helpful post, thanks a lot..

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.