Skip to content

Instantly share code, notes, and snippets.

@fovoc
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save fovoc/b21144f83e76ba02476b to your computer and use it in GitHub Desktop.

Select an option

Save fovoc/b21144f83e76ba02476b to your computer and use it in GitHub Desktop.
Random Post Button
<?php
add_action('init','random_post');
function random_post() {
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment