Snippet: Get Random Post Link

Code snippet to fetch link of a random post of your blog.

function get_random_post_link ()
{
	$args = array(
		'posts_per_page' => 1,
		'ignore_sticky_posts' => true,
		'post_type' => 'post',
		'orderby' => 'rand'
	);

	$query = new WP_Query($args);

	if (!$query->have_posts()) return '#';

	$query->the_post();
	return get_permalink();
}

Credits: Michal

Leave a Reply

Your email address will not be published. Required fields are marked *

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>