WordPress.org

Ready to get started?Download WordPress

Forums

ONE link to ONE random post (13 posts)

  1. Jack
    Member
    Posted 8 years ago #

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

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

  3. davidchait
    Member
    Posted 8 years ago #

    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...).

    -d

  4. Jack
    Member
    Posted 8 years ago #

    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? :( :( ...

  5. Chris_K
    Member
    Posted 8 years ago #

    The Related Posts plugin could be another option.

    http://www.w-a-s-a-b-i.com/archives/2006/02/02/wordpress-related-entries-20/

  6. forceagainstsomething
    Member
    Posted 8 years ago #

    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:

    <?php
    /**
    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'];
    $randpost_counter++;
    $stopper++;
    if ($stopper > 20) break;
    }
    $link = get_permalink($randPostID);
    return $content . "
    Random Post";
    }

    ?>

    - Sean
    http://www.headzoo.com

  7. forceagainstsomething
    Member
    Posted 8 years ago #

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

    return $content . "
    Random Post";

  8. forceagainstsomething
    Member
    Posted 8 years ago #

  9. davidchait
    Member
    Posted 8 years ago #

    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().

    -d

  10. geoffe
    Member
    Posted 8 years ago #

    Here's a good Random Posts Plugin.

    And this Customizable Posts plugin does random posts and more.

  11. forceagainstsomething
    Member
    Posted 8 years ago #

    "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.

  12. ruzel
    Member
    Posted 7 years ago #

    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?

  13. ruzel
    Member
    Posted 7 years ago #

    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
    http://www.banapana.com/monkeylink-08b-wordpress-plugin/

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags