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. Hello, on my WP 2.1.3 the plugin, once activated, doesn’t appear in the Presentation/Sidebar Widgets tab. Any suggestions?

  2. Unfortunately, I am. After activating the plugin, in the Presentation/Sidebar tab of my site the Random Post widget simply doesn’t appear.

  3. Well, unfortunally it is true, i do not see it as widget when i try to post..maybe i am doing something wrong i have no clue. Can you give us a screen shot of how it looks the whole page with the randompost widget open?

    Thanks

  4. @Astirt: It is not a widget used in posts. It is a widget to display a list of links to your old posts, on your sidebar. You can see the widget in action at the front page of this blog (bottom of column 3). Ctrl-F and search for ‘Random’. In the administration area, it should be available in the Presentation/Sidebar widgets page as a draggable item.

  5. Ady,

    Good plugin. One bug is that you are also picking up pages. You need to add to the where clause to check for post_type of ‘post’ for WP 2.1+.

    Mike

  6. I have the same problem. Where ever I put the widget on my side bar (wordpress 2.1) the rest of my bar just stops loading and it whole page is messed up – Any way to fix this?

    Thanks – I’d really love to use this.

  7. To those who are still having problems with this plugin, try the latest version (1.3). And if you still have problems, activate & place the plugin then email me your blog URL (if you don’t want to disclose) so that I can analyze the issue.

  8. Hi Ady,

    thanks for the plugin! Exactly what I’m looking for *but*

    With a lot of posts (>1200) the query is too slow to use. The sidebar hangs up during page load while the query executes.

    It looks like we’re SELECTing everything in the DB and then shuffling it?

  9. @air: The query is simply utilizing MySQL’s built-in random function:

    … ORDER BY RAND() LIMIT $numPosts

    Internally I think that is the case, like what you mentioned. I will explore other possibilities to retrieve random records and update.

Comments are closed.