Random Post snippet
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.
Nice! Good idea about the 307 redirect. I’ll give it a try – John
[…] : http://ottopress.com/2011/random-post-snippet/ [!] Report this snippet Processing your request, Please […]
[…] 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 […]
Handy snippet indeed! Thanks!
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?
Try the “category__in” parameter.
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.
Very nice.
Why the foreach() instead of just
?
No reason. Queries return arrays, and foreach is a pretty normal way of dealing with arrays. 90% of code I’m writing falls into a pattern. When I write queries, then I loop through the results, even if there’s only one result.
Okay, makes sense – just plain coding-habits 🙂
You’re a modern superhero!
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! 🙂
[…] “Random Post Snippet,” Otto on WordPress […]
[…] “Random Post Snippet,” Otto on WordPress […]
[…] “Random Post Snippet16,” Otto on WordPress […]