WordPress: Display Recent, Related or Random Posts in Your Sidebar

You may have noticed that I have dynamic sidebar that changes slightly as you browse the different parts of the site. For example, I have random posts in my home sidebar, while this changes to related posts when browsing a specific post.

One visitor emailed me recently asking me if I could tell her how I managed to display recent, related or random posts in my sidebar.

Well, I neither use a plugin nor SQL statements for this part.

I used the conditional tags to check the current page and to execute the right part of the code.

The Code in Pieces

I divided the code into three pieces, so you can understand it easily.

Recent Posts

<div class="widget">
<h2>Recent Posts</h2>
<?php $posts = get_posts('numberposts=3'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
<?php } ?>

The above code picks the latest three posts. You can change the number of posts displayed by changing number in get_posts('numberposts=3'); to whatever you like.

Related Posts by Category

<div class="widget">
<h2>Related Posts</h2>
<?php $posts = get_posts('numberposts=3&category='.$category->cat_ID.'&exclude='.$current_post);
foreach($posts as $post) {
<li><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>"><?php the_title() ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
<?php } ?>

We used get_posts('numberposts=3&category='.$category->cat_ID.'&exclude='.$current_post); to make all the business easier. $category->cat_ID pulls the current post’s category, exclude='.$current_post makes sure to exclude the current post from the related ones.

You may use related posts by tag alsoWP Recipes

Random Posts

<div class="widget">
<h2>Random Posts</h2>
<?php $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
<?php } ?>

Our code uses orderby=rand. So WordPress will pick 3 random posts from the database every time our site loads.

Final Thoughts

The above codes can be merged together and displayed on the sidebar using the WordPress conditional tags.

If you have enjoyed this post or have any questions or recommendations, please share your views in the comments section.


Tags: , , , ,

19 Responses to WordPress: Display Recent, Related or Random Posts in Your Sidebar

  1. Hon tap

    So cool. I love the related post code, so simple and effective. Thank you very much.

  2. Pingback: Gutes tun » Blog Archive » Wordpress-Hack: So zeigen Sie zufällige Postings in der Sidebar an

  3. Pingback: Gutes tun » Blog Archive » Wordpress-Hack: Die neusten Postings in der Sidebar anzeigen

  4. Accounting Teacher

    Where this code shall be added?

  5. Jamal

    Accounting Teacher
    Usually in the sidebar of your blog sidebar.php. Also you can put it anywhere out site the loop.

  6. Mikel

    Thanks. This is a great tut.

  7. Dave Nalle

    I like the idea of these widgets, but sadly none of them seem to work. All any of them produces in my sidebar is “—Posted on ”


  8. Minea Adrian

    Please help me, I think at something else, I don’t know how to write the PHP code. I want to add a section in sidebar “My best posts” (5 or 7) random selected URLs from a predefined list of 40-50 URL’s manually added by me in a separate PHP file.

    There must be done with two things:

    1. Line of code from the box <?php wp_get…
    2. PHP file containing the URL list added by me one per line and the little script of course!

    Can you help me?

  9. MK

    is it possible to use the random posts but to get a set of posts from a specific month

  10. Jamal

    Mikel, I am very glad that you liked it. 😉

    Dave Nalle, please double-check your changes. The code published here is the same I used in my current sidebar.

    Minea Adrian, let me know what type of CMS you are using.

    MK, Sure, you can use random posts based on specific month. Replace this line:

    < ?php $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...


    < ?php
    $posts = get_posts('monthnum=8&orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...

    Just we added monthnum=8 parameter. This parameter returns posts dated August regardless of the year posted.

    If you want to display posts published in July, 2009 randomly, you need to expand your code like this:

    < ?php
    $posts = get_posts('year=2009&monthnum=7&orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...

    The above code will return 3 random posts published in July, 2009.

    If you want to make your site more dynamic (e.g. if you want to display random posts from the current month and year), use the code below instead.

    < ?php
    $current_time = getdate();
    $posts = get_posts('year=' .$current_time["year"] .'&monthnum=' .$current_time["mon"] . '&orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...

    The above code displays 3 random posts published in the current month and year (e.g. as the time of writing August, 2009).

    Good luck.

  11. Joe

    I’m just getting the title and ‘- posted on’. Can this be added to my sidebar via a text widget? Or must it be added within the actual sidebar.php file?
    (I’ve added other html links using the text widget but I don’t know if php works in this area) . Thanks. Nice post too btw.

  12. Jamal

    Joe, PHP code doesn’t work in the HTML/text widget. So you need to open your sidebar.php file and paste your code there.

    Hope it works for you.

  13. Pingback: Ultimate resources wordpress development tutorials | blogfreakz.com

  14. eDealz

    Hi all,
    I have been trying to get the wordpress recent posts to work on the homepage of my site (not wordpress site) and have failed many times trying with all the code from wordpress and from here too.

    I have added the reuire wp blog header function and used the code within the page to display results. I do not get any errors – however the fact is that i get nothing?

    The site uses smarty – anyone any ideas what the problem could be?

  15. Jamal

    eDealz, this is a WordPress code and all the functions are involved in the WordPress core code. So this code has no way to work on non-WordPress platforms.

  16. Pingback: links for 2009-11-17 « Free Open Source Directory

  17. Gary Chambers

    Hi just starting on WordPress could you advise on how to get this code working on the sidebar of my main site. Many Thanks

  18. Dallin

    Hey guys I found a quick, free and easy answer to displaying random posts. http://wordpress.org/extend/plugins/random-posts-widget/ I just downloaded that plug in and it displays 5 random post in my side bar. Just thought I would share what i found. Thanks for the great post J Mohamed



  19. Ivan Seminole

    I found this upon, an individual creating this, the idea had been what exactly for you to be searching for!

Leave a Reply

Your email is never published nor shared. Required fields are marked *

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Login to your Account

I forgot my password.