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

  2. nurandi says:

    How to display summary for each random post title?

  3. Katie Shades says:

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

  4. vaibhav says:

    How can i display the post in horizontal manner

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

  6. wpbeginner says:

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

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

  8. 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?

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

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

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