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. Here’s the content of the error, all appearing on my front page. “From the vault” is my title for the Random Posts Widget, and the error appears above it.

    WordPress database error: [Table ‘evan_wrdp1.wp_post2cat’ doesn’t exist]
    SELECT wp_posts.ID FROM wp_posts, wp_post2cat WHERE wp_posts.post_status = ‘publish’ AND wp_posts.post_type = ‘post’AND wp_post2cat.post_id = wp_posts.ID AND wp_post2cat.category_id = 9

    Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/evan/public_html/wp-content/plugins/randomposts.php on line 87

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘)’ at line 1]
    SELECT wp_posts.post_title, wp_posts.ID FROM wp_posts WHERE)
    From the Vault

  2. Is it possible to install this on a non-Wordpress site but linking to a WordPress Blog. In other words, I want to display random posts of my blog on a completely different site that is not a blog and does not have WordPress installed.

  3. Hi there.
    I was wondering on if there could be an option for an excerpt (Y/N) with a selectable X number of words. The excerpt would be in plain text. I could be nice for those with little on their sidebar. Thanx for a great plugin

  4. First off, GREAT plugin!

    Would it be possible to put this in the body of my blog? Actually insert it in the HTML? I’d love to able to put this at the bottom of my posts. Is there any way I can do this?

Comments are closed.