Random Post Order in WordPress

Randomising your post order in WordPress should be as easy as hitting Command-Shift-R to hard refresh your page, but depending on your theme set-up, there are a number of approaches to consider, some more random than others.

WordPress CMS and themes by Special OperationsWelcome to the life of a web-developer. “Can you make my bing-bong random?” says the client, but adding, “…this won’t cost me anything extra will it? I mean, after all, you can just Ctrl-Click your fingers and make it happen?”

Yes, some things are very simple in development and programming, but that doesn’t mean that hours, days, weeks and years do not go into making them so. Like random post order in WordPress. It’s truly simple once you know how, but knowing how could, unguided, take hours of research…

The Plugin Approach

One of the great things about using WordPress as a CMS—perhaps even the greatest—is that there is more than likely an already written plugin for whatever feature you seek. More than likely, but in the case of random post order, there are some limitations to consider…

Custom Post Order Plugin

The Custom Post Order Plugin is an excellent plugin for customising the order of your WordPress posts, with a variety of ordering options including:

  • post date;
  • post title;
  • post author;
  • last time modified;
  • post slug;
  • ascending or descending order.

Sadly however the Custom Post Order Plugin doesn’t allow for randomisation, so we will have to scratch it off our list.

Advanced Random Post Plugin

Only “officially” compatible with WordPress 2.3.3, and only at the time of writing developed to version 0.2, Daniele Salamina’s Advanced Random Post Plugin still works exactly as advertised, albeit with a few limitations:

  • it only allows a maximum of 5 random posts;
  • it isn’t compatible with sticky posts (to be fair these didn’t exist in v2.3), although it allows you to select a post position from where the randomisation takes effect, which may in some circumstances be an acceptable workaround;
  • it wreaked havoc with a jQuery based slider, in this particular case the slider included in WooThemes’s Daily Edition theme (and many others).

WordPress moves, and updates, extremely quickly, and early 2008—the last time this plugin was updated—is in WordPress terms a very long time ago. Therefore, what is more than likely a partial incompatibility between the CMS and plugin—WordPress 2.9 is a very different beast indeed from v2.3—may lead you to scratch this plugin from your set-up completely.

Random Posts Plugin

Rob Marsh’s Random Posts Plugin is an excellent plugin from a family of excellent plugins (Popular Posts, Similar Posts, Recent Posts), with true everything-plus-the-kitchen-sink customisation possible. Able to be deployed as both a sidebar widget and a PHP function, this plugin possesses a near complete language for virtually limitless customisation, including the capacity to render HTML and PHP.

Without extensive customisation however, the Random Post Plugin is limited to generating a single iteration list of posts, outside of the WordPress Loop, and therefore is not a solution if it is the The Loop itself which you are attempting to randomise, i.e. make random all your WordPress posts, potentially across multiple ‘pages’.

The Template Approach

While there is no random post order option available (yet) in WordPress’s increasingly well-designed Dashboard, as of v2.5 randomisation has been included under the hood as a part of the template language, and can be invoked as a parameter of both the get_posts and query_posts template tags.

This is a far more elegant solution than a plug-in, as it utilises a functionality of WordPress that is already built-in, and doesn’t add an extra layer code with the potential for conflict, bloating and deprecation.

Random Posts Template Example

 $args = array(
	'numberposts' => -1,
	'post_parent' => $post->ID,
        'orderby' => 'rand'

More information on WordPress Templates

More detailed documentation on using both query_posts and get_posts can be found at WordPress.org.

Command-Shift what you say?

Yes, all right, if you’re coding for the Evil Empire, Ctrl-Shift-R or F5 will also do the needful.

Tags: , , , , , ,

Fatal error: Call to undefined function do_sociable() in /home/sensitiv/public_html/wp/wp-content/themes/specialops/single-blog-post.php on line 54