WordPress Plugin: Random Posts Widget

My better half was complaining that she can’t find a suitable plugin for displaying random posts on her sidebar. I tried to look myself, and there were some references to the plugin but nowhere to be found. One good plugin I found is Random categories with random posts by Mike Stickel but it uses categories which is not what we are looking for. Also, it requires tinkering with PHP codes which is not suitable for less technical bloggers.

So I went to analyze his code for a little while. What the heck, I’ll just create a simple widget version with no categories involved. So I took about 30 minutes to write this simple code with not so many options. It is a widget and configurable in the Sidebar Widgets page:

Random Posts Widget configuration

  • Widget title: the title of the widget
  • List types: ul for bulleted list, p for paragraph, br for paragraph with line breaks
  • Before title and After title: you have to find out what your theme uses before and after the widget headers. A simple way to do this is to identify a currently existing widget in your site and viewing the source. Find the title and see what is printed before and after. For example it might show as <h2>Widget Title</h2> which is used by most themes.
  • Post count: Number of random posts you would like to be displayed

Basically that’s it. It’s really a simple widget.

I don’t like to reinvent the wheel but since I can’t find any I just have to create one myself. Most probably I will have not so much time to maintain this plugin, but it is suitable if you are looking for a (very) simple plugin to display random posts.

You can download it from WordPress Plugins Directory: Random Posts Widget, or view online here: randomposts.php

Current Version: 1.5.0

Installation

Copy the file to the wp-content/plugins/widgets directory and activate it in the Plugins page. Then drag and configure it in the Sidebar Widgets page.

Warning: Will only work on widgets enabled blogs.

Change Log

  • 07-May-2007: Fixed a bug of displaying widget before and after tag
  • 14-Jun-2007: Fixed a bug where pages are also selected. Now it only displays posts.
  • 26-Jun-2007: Removed before and after configuration options, now taken from theme settings. Was broken for some themes with embedded class in the header tags. Reported by Guohua. Thanks.
  • 03-Aug-2007: Overhauled entire engine to optimize queries. This has been tested on a test blog with 50,000 posts on an old machine (Pentium III 600MHz) in maximum 3 seconds. Also removed duplicated results and added category to choose.
  • 04-Aug-2007: Fixed a bug where wrong permalink was displayed.
  • 10-Aug-2007: Fixed a bug where if 1 post count is selected an error occurred.
  • 27-Sep-2007: Added ability to detect database version (WordPress 2.3 has database changed) while maintaining backward-compatibility with 2.2.x
0 Shares

100 thoughts on “WordPress Plugin: Random Posts Widget”

  1. @air: thanks. I was thinking of the same solution but I am just not sure of what effetc does the extra queries have. Will try this on development machine first.

  2. Would it be possible to restrict posts by Category. I have a movie review site and I’d like to only include reviews and not other posts.

  3. If I may add another suggestion: I sometimes get the same post listed twice (not often but it happens). Can you prevent that from happening?

  4. Yeah, me too. I have also noticed that the same post can show up several times. Not a big problem, though, but still…

    Great plugin man! I’ve been looking for something like this for a while!

    Cheers!

  5. Hello all.

    I’ve just uploaded version 1.4 of the plugin to the plugin directory. I’ve taken a different turn than the suggestions below as WordPress stores data in a very different way. However I have succeeded to rewrite the algorithm and achieved very good performance. A category can now be selected, and no more duplicates. Do let me know if you have any problems.

  6. I have successfully uploaded it & put the widget in my sidebar. It appears in my sidebar alright but when I click it, it’s not taking me to the right post. Any ideas why?

    Thanks!

  7. Oh No! Version 1.4.1 contained a bug where a wrong permalink is displayed. Thanks to Melanie for noticing this. I have fixed it and uploaded as version 1.4.2. Grab it once updated on the WordPress plugin directory.

  8. WOW! It’s working like a champ now!
    BIG BIG THANKS Ady!
    I used to have Way Back widget in there & I replaced it with this one. It’s better I think! Much much better!
    Great Job!

  9. This sounds good, but the one thing I cannot find anywhere is an example of this in action. When I click on what’s supposed to be an examples, I get to see the actual code for this widget, not the end result. I can’t view a working example of the collapsible archive widget either… I looked and looked on the main page, where it’s supposed to be, but I must be missing it?

  10. @Wendy: You missed them. They are all in the front page. The random posts are titled “Random Posts” while the archives are labelled “Archives”. They are both at the bottom of the 3rd column, just before the “Recommended Products” (Amazon). Can you find them?

  11. Thanks, ady, yeah, I’m a goober… I see it… NOW. The archives feature didn’t look how I expected it to — though yeah, it does what I was looking for. I didn’t see the Random at all. Maybe my brain was having a hiccup.

    BTW, for kicks, here’s the WordPress project I’m working on at the moment: http://www.inception-magazine.com/zine/ . I did my first WP project last month, http://www.stormconstantine.com and was so thrilled with how it turned out I switched hosts on the zine site just so I could redo it (in all its static HTML, CSS-less horror) on another host with WP and PHP.

  12. el johnny: I don’t think it’ll work but you can try uploading the plugin and activating it. It can be called with < ?php echo ara_random_posts($before_title,$after_title); ?>. Change the two variables to whatever your plugin uses for the titles. However one problem I foresee is the ability to set options as this is controlled by register_widget_control. Try it out and let me know. Just out of curiosity, what version are you using? Newer versions should have widgets built-in. 🙂

Comments are closed.