WordPress Random Posts

How To Create A Random Post Link and Redirect Visitors To Random Posts On WordPress

Create a random post link and redirect WordPress visitors to a random post on your WordPress blog or website using simple PHP functions and variables.

Creating A Random Post Link In WordPress

WordPressRedirecting WordPress users and viewers to a random post on a WordPress website can be done in many different ways. We have already discussed building a PHP page file to create the link: yourdomain.com/random which will redirect WordPress visitors to a random post, but there is a more simple alternative to this method which can be used in opposing situations. Utilizing the get_var() function, and storing the results inside a $randomPost variable, and then echoing the link to point at the random post URL stored in the $randomPost variable.

This method allows users to place a random post redirection link anywhere inside WordPress PHP template/theme files.

Copy and paste Copy and paste the snippet below into your chosen WordPress PHP template/theme files.
[Normal_Box]

<?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>';
?>

[/Normal_Box]
Change “Random Post” to your preferred anchor.

Sean Doyle

Sean is a tech author and security researcher with more than 20 years of experience in cybersecurity, privacy, malware analysis, analytics, and online marketing. He focuses on clear reporting, deep technical investigation, and practical guidance that helps readers stay safe in a fast-moving digital landscape. His work continues to appear in respected publications, including articles written for Private Internet Access. Through Botcrawl and his ongoing cybersecurity coverage, Sean provides trusted insights on data breaches, malware threats, and online safety for individuals and businesses worldwide.

Post navigation

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.