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…).
-d
Thread Starter
Jack
(@moxie)
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? 🙁 🙁 …
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
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().
-d
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
http://www.banapana.com/monkeylink-08b-wordpress-plugin/