Support » Plugins and Hacks » ONE link to ONE random post

ONE link to ONE random post

  • Perhaps I’m overlooking a plugin that does this, it seems so logical it must exist =).

    A plugin that gives me ONE link (text or image) to ONE random post of the past. A ‘jumper’. A link from which the visitor can keep jumping at random from one post to the other. I’ve seen it before and it’s fun to use, especially when being new to a site.

    If it doesn’t exist would someone be willing to create it? I’m not into PHP, but it seems to be rather easy? Count the total number of posts, random(‘number’) and the creation of the link?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator Samuel Wood (Otto)


    WordPress.org Tech Dude

    Look towards the bottom of this thread: http://wordpress.org/support/topic/40818

    CG-Samecat will do it for picking a random post within a given category, up to a certain limit. The issue is the query overhead — you really need to do one query to tell you how many posts there are (and possibly retrieve postIDs), and another to do the random-pick. And, along the way, exclude the current post. Samecat can do that within the context of a category (or multiple categories, though I’ve never tried ‘all’!).

    The issue for bigger sites is the two added queries are a hit per pageload, and you don’t usually want that. So you need something caching some results for some period of time (say, a day, or a few hours…).


    Seems like what I was looking for 🙂 Thanks for the tip!

    David: I really would like to have all categories included.

    By the way, I forgot that removing some posts means you can’t just pick a random number between 1 and 600 because this could end up with a deleted post =). So it’s not as simple as I thought. But one query should do it imho. Just getting all the postID’s, put them in an array, and then exclude the current ID, count array and pick one from it.

    In theory I know what should be done, but PHP? 🙁 🙁 …

    The Related Posts plugin could be another option.


    MySQL already has a RAND() function that you can use for this. This plugin only requires 1 query. Here is the whole plugin. Just copy and paste into a file with a PHP extension, and put it in your plugins directory:

    Plugin Name: Random Post
    Author: Sean Hickey
    Description: Display a link to a random post
    $randpost_results = $wpdb->get_results(“SELECT ID FROM $wpdb->posts WHERE post_status = ‘publish’ ORDER BY RAND() LIMIT 20”, ARRAY_A);
    $randpost_counter = 0;
    add_action(‘the_content’, ‘randpost_theContent’);
    function randpost_theContent($content) {
    global $randpost_counter, $randpost_results, $post;
    $thisPostID = $post->ID;
    $stopper = 0;
    $randPostID = $thisPostID;
    while ($randPostID == $thisPostID) {
    $randPostID = $randpost_results[$randpost_counter][‘ID’];
    if ($stopper > 20) break;
    $link = get_permalink($randPostID);
    return $content . “
    Random Post“;


    – Sean

    Oops, the last line in the function should be this:

    return $content . “
    Random Post“;

    Warning: there are multiple articles on how ORDER BY RAND() is pretty evil, and NOT something you want to do constantly. You are better off either using a subquery for performance, or similarly pre-querying the number of rows and producing a query for the random post then using “LIMIT “.$somerandomnum.”,1″ in order to restrict it and do it fast.

    Note the article I linked is just one of many that (way at the bottom) analyzes the actual db-crushing performance of ORDER BY RAND().


    Here’s a good Random Posts Plugin.

    And this Customizable Posts plugin does random posts and more.

    “Warning: there are multiple articles on how ORDER BY RAND() is pretty evil, and NOT something you want to do constantly.”

    To be perfectly honest, anything that the RAND() function does is likely a drop in the bucket when it comes to WP’s poor queries. A RAND() query is like a bad pebble in a big pile of bad rocks when it comes to WP.

    If anyone out there is listening (especially forceagainstsomething): how could I modify this random link post so that it would work within the loop. In other words how could I wrap a random link around some text in a post? Would it be necesary to create a template tag like “<–more–>” that would be substituted for a random link or is there a simpler way?

    Hello again, so I ended up writing the plugin meself. What this plugin will allow you to do is create a contextual random link. In your post, you surround what want you want randomly linked with HTML comment tokens:

    <!--ml-->random link text<!--eml-->

    ml is short for monkeylink since that’s what I call the plugin. (eml is short for end monkeylink). The plugin transforms that part of the post into a randomly generated link to a wordpress post.

    Here’s the plugin URL

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘ONE link to ONE random post’ is closed to new replies.