WordPress Guy

How to make a random post button

Ever wanted to have a “Random Post” link or button in your sidebar but you don’t know where to start? Well, today I’m going to show you how to do it.

The principles

As you may know already, WordPress posts are stored in a MySQL database. For us to obtain the URL to a random post from that database, we need to run a MySQL query. It’s pretty simple actually, thanks to WPDB, a built-in database class that comes with every WordPress install, we don’t need to bother about database names, passwords, connections and disconnections, etc. We just need to run the query and echo the result.

The MySQL query

Our database query needs to look inside the posts table and select one random post from the published posts. This is what the query looks like:

SELECT guid FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY RAND() LIMIT 1

Let me explain how exactly this MySQL query works:

  1. SELECT guid - Select just the guid column value, which is where the URL to the post is located.
  2. FROM wp_posts - From the posts table
  3. WHERE post_type = 'post' - Since attachments are also saved in the posts table, we need to specify that we just want posts
  4. AND post_status = 'publish' - We just want the published posts, not scheduled posts, not drafts
  5. ORDER BY rand() - Get them in random order
  6. LIMIT 1 - And get just one result

The PHP code

Now, let’s type the PHP code. As I said, we’re going to make it simple by using the WPDB class, which comes bundled into WordPress. This is our code:

<?php
$randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
echo '<a href="'.$randomPost.'">Random Post</a>';
?>

In the first line of the code, we’re running the database query using the WPDB get_var() function, and storing the result inside a $randomPost variable. Then, in the second line, we’re echoing the link, pointing to the URL stored in the $randomPost variable.

Note I’m using $wpdb->posts instead of wp_posts, that way, we make sure that we’re selecting the right table, since you could have changed the table prefix (which by default is ‘wp_’).

That’s it for today. I hope you enjoyed the mini-tutorial.

Comments

  • If you want to do this using the WordPress API, it’s fairly straightforward:

    'rand', 'showposts' => 1)); ?>
    <a href="">Random Post

    I would also caution against using the GUID field of wp_posts as a permalink. At the moment, it happens to be a permalink to the post, but as a globally unique ID, it could just as well be a string of characters like “3F2504E0-4F89-11D3-9A0C-0305E82C3301,” and it may end up that way in future versions of WordPress.

    Austin May 27th, 2008 at 10:33 pm
  • The filter ate my code snippet. Trying again:

    'rand', 'showposts' => 1)); ?>
    <a href="">Random Post

    Austin May 27th, 2008 at 10:35 pm
  • Erg. Well, here’s the relevant code, and I’ll stop spamming your comments.

    The query:

    query_posts(array('orderby' => 'rand', 'showposts' => 1));

    The part that prints the link:

    the_post(); the_permalink();

    Austin May 27th, 2008 at 10:39 pm
  • @Austin: Indeed, that would be the right way to do it, I just felt it would be easier to just do a database query… Avoid a second loop, problems, you know… ;-)

    Wessley Roche May 27th, 2008 at 11:29 pm
  • Matt Mullenweg created a plugin which makes the whole process easier. http://www.PaulStamatiou.com uses it (random post link: paulstamatiou.com/?random), but I’m not sure if he uses Matt’s plug-in or coded it himself.

    Dean July 19th, 2008 at 10:11 am

Your Comment