Random Posts
published
I wrote up a quickie hack for Sushubh to pull random blog entries from the database. To make it generally useful, I wrote it as a function that takes two arguments:
random_posts ($catID = 0, $count = 4);
By default, it’ll grab four random posts from any category. Give it a category ID and/or a specific number of posts to pull in order to change it.
If you know you’re not going to use all of the posts’ data, you could tweak the last SQL query to grab just the bits you need, instead of everything, thereby easing the load on your database.
Oh yeah, this hack does not honor my per-post user-level restrictions modification. If you’re using that, this hack will happily display all your restricted posts.
Not the most useful of hacks, but maybe someone somewhere will put it to good use.
UPDATE: I’ve made this into a proper plugin for WordPress 1.5 and above. To use it, simply install and activate the plugin, then insert the following code in your template:
<?php echo "<ul>"; $rp = random_posts($catID, $count); foreach ($rp as $p) { $title = get_the_title($p); $link = get_permalink($p); echo "<li><a href='$link' title='$title'>$title</a></li>"; } echo "</ul>"; >
Download random-posts.zip!