Wrote this quick WordPress code snippet at WordCamp Louisville. It makes a /random/ URL on your site which redirects to a random post. Thought some people might find it useful.

Not a perfect little snippet, but gets the job done. Note the use of the little-used 307 redirect for temporary redirection. This is to make browsers not cache the results of the redirect, like some of them might do with a 302.

add_action('init','random_add_rewrite');
function random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}

add_action('template_redirect','random_template');
function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
               foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
}

There’s plugins that do this sort of thing too, but this is such a simple little thing that it doesn’t really need a big amount of code to do.

Edit: Added get_permalink() optimization from @Raherian.

Shortlink:

15 Comments

  1. Nice! Good idea about the 307 redirect. I’ll give it a try – John

  2. [...] You are here: Home > News > Otto with a simple random post URL snippet PermalinkOtto with a simple random post URL snippet var addthis_product='wpp-262';var [...]

  3. Handy snippet indeed! Thanks!

  4. I like the idea behind this, however I would like to exclude a certain categories. How would I modify the code to do that?

    Could I use something like this? And have to include the categories I want rather then excluding them?

    $args = array( 'numberposts' => 1, 'orderby'=> rand, 'category' => 1,3,7,8 );
    $posts = get_posts( $args );
    
  5. isn’t it “posts_per_page” instead of “numberposts”?

    • The get_posts function can take either parameter, actually. The “numberposts” parameter is the older version of that, still there for backwards compatibility. I tend to use it whenever using get_posts, but either will work.

  6. Very nice.

    Why the foreach() instead of just

    wp_redirect( get_permalink( $posts['0'] ), 307 );

    ?

  7. You’re a modern superhero!

  8. By the way,

    I implemented the code in my website and it is working like a charm! I now have a button leading to a new random post from the same category (see code below)
    ——————————————————————
    add_action(‘init’,’random_add_rewrite’);
    function random_add_rewrite() {
    global $wp;
    $wp->add_query_var(‘random’);
    add_rewrite_rule(‘random/?$’, ‘index.php?random=1′, ‘top’);
    }

    add_action(‘template_redirect’,’random_template’);
    function random_template() {
    if (get_query_var(‘random’) == 1) {
    $args = array( ‘numberposts’ => 1, ‘orderby’ => rand, ‘category__in’ => 11 );
    $posts = get_posts( $args );
    foreach($posts as $post) {
    $link = get_permalink($post);
    }
    wp_redirect($link,307);
    exit;
    }
    }
    ——————————————————————

    Now I have a question:
    What do i need to change to be able to add another button to do the same in yet another category?
    I guess the code should be the same but i need to chane the “random” at the end of my url by “random1″ or something, and change the category number.
    I am trying to find the bit of code i need to change to affect only the word i should put at the end and the category.
    Would i need to add the whole snippet below the first one?

    Thanks for the help! :)

  9. [...] “Random Post Snippet,” Otto on WordPress [...]

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Need to post PHP code? Wrap it in [php] and [/php] tags.