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. my goodness… i’ve been looking for this type of plugin throughout the whole worldwideweb – and i found this post from google. you didnt tell me you have this plugin.

    thanks a bunch ady

  2. Well, sometimes the thing are you looking for is under your own nose and you just don’t realize it 😉

    Enjoy… you’re lucky you can ask for face to face support for this plugin if you have difficulties LOL.

    Hmmm… I must find time to enhance this plugin. It’s long overdue.

  3. Thanks for this plugin.

    FYI, I used “php echo ara_random_posts($before_title,$after_title); ” with WordPress 2.6.2 and it seems to work fine.

    I’ll let you know if I have to face issues.

  4. excellent plugin! fantastic work…

    one quick question, ady–how difficult is it to increase the number of results to 15 instead of 10 max?

    i tried nosing around the code but i’m no programmer i’m afraid. 🙁

    thanks!

    ken

  5. Hi, i installed your plugin but as i’m using jLanguage to make posts in english and spanish your plugin show the post titles as [english]Title[/english][spanish]Titulo[/spanish].

    That is the syntax jLanguage uses to know which language to show but the default Recent Posts widget of WordPress show the titles correctly.

    Is there anything you or i can do to solve this? Many thanks.

    Regards.

  6. This is very user friendly – thank you! I have gone through a number of plugins to try and find one that will also let me include an excerpt. Would it be difficult to include that in this plugin? I can edit it, but I don’t know where.

    Thanks so much!

  7. Im on wordpress 2.8.6 and your widget has destroyed my blog

    I get this code when i try to acess any part of it, including admin

    Fatal error: Cannot redeclare widget_ara_randomposts() (previously declared in /websites/123reg/LinuxPackage21/mi/sc/mu/miscmusic.net/public_html/wp-content/plugins/random-posts-widget/randomposts.php:198) in /websites/123reg/LinuxPackage21/mi/sc/mu/miscmusic.net/public_html/wp-content/plugins/random-posts-widget/randomposts.php on line 198

    Please get back to me

    Thanks

    1. Delete the plugin file randomposts.php (or the whole folder). As mentioned in http://wordpress.org/extend/plugins/random-posts-widget/ this plugin has only been tested up to 2.3 (although it worked up to 2.7). After you delete you should be back to normal and be able to access your site normally.

      I have intentions to update this plugin but still struggling to find time. WP 2.9 has a lot of changes as well, and this plugin definitely needs to be updated as it will be broken.

      Thanks for reporting the issue.

  8. Hi. This might sound odd- but i’ve uploaded it and activated it and it is nowhere to be seen when i then view plugins or widgets. Hmmm. I’ve then tried to upload again and there is an error saying that it already exists.

    Any ideas? I’m using WP 2.9.

  9. This is a great plugin. I’ve had it on my site for many months. I set it and forget it.

    But I’m with reader ken, above, who would like to increase the number of displayed posts to more than 10.

    I’m willing to dig into the style sheet and make changes.

    Thanks.

  10. Hello!

    This plugin is very nice. But I´m going mad trying to find a plugin or code to randomize post within a specified tag or category..
    For exemple,

    I have 3 posts: Number 1, Number 2, Number 3 under “Lucky Numbers´category”

    And a link somewhere: “whats your lucky number today” – so when clicked, the random code is able to retrieve a post within “Lucky Numbers´category” only.

    It could be very usefull if able to retrive posts within Tags, for a more specific content control.

    If someone else know some plugins or wp/code for this, please let me know!

  11. Hi.. I loved this widget. Thanks a million. Just one question. Can I make it NOT link up to the original article?

    I just want the widget to show up the posts and thats it.
    Please let me know.

    Thanks again 🙂

  12. @Diana….and to all others it may concern (and of course Ady)

    I was also looking for a plugin that could display some random posts from a specific category, as to me this is great for more dynamic sidebar content and CMS based sites. So after looking around a bit I found this handy little plugin, and another one called Get-a-Post.

    Still not quite what I needed so I’ve adapted Ady’s code in a way that allows it’s output to be plugged straight in to the get-a-post plugin! Used in a suitable code loop and voi-la, multiple random posts wherever you need them – although familiarity with PHP is needed so this is most useful to template designers and coders.

    I haven’t ‘published’ it yet as much of the code is Ady’s, and with it’s dependence on the Get-a-Post plugin I felt I should inform both authors first.

    Also this would be my first plugin so I don’t really know what to do in terms of making it available to others, however as long as both respective authors are happy then I’ll see what I can do…

    Thanks

Comments are closed.