Codeable info

Implementing A Random Post Link

Posted on by in WordPress Tutorials

Want your readers to browse more of your blog posts? If so, you might want to make browsing your blog a little more interesting than just navigating through post categories and archives.

A great way to spice things up (as far as site navigation is concerned) is to throw in a “random” post link that will jump the reader to a completely random post hosted on your blog/site.

The information for each WordPress post is stored in a local MYSQL database, and unlike most website applications and builds WP automatically has an integrated database (WPDB) which is set up during install. This means that there is no extra tinkering or source file editing required (besides adding in the random post link).

The Random Post Link Tag

WPDB makes things extremely easy; all we need to do is implement the code for the random post link which will use a MYSQL query. The following code can be added anywhere you would like the link to appear:

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

The following options can be changed in the code above to customize how the random link works:

AND post_status = ‘publish’- This basically tells WordPress to only select from posts with published status.

LIMIT 1- This decides how many total posts are displayed in the final results. If you would like more posts to be displayed you can change the number accordingly.

Style It Up

Remember to customize the style and output of the link before implementing the code on your WordPress enabled blog or site. As of now the link will only appear as text, in the “Random Post” form. If you would like to actually display the link as a button, you will need to implement the proper code before adding it to your site or blog.

Otherwise you now have a working link that will bring your readers to a completely random post on your WordPress blog or site.

My work here is done. Enjoy!

)
Codeable info

Comments (7)

Comment by Robert says:

Hi,

How would I go about using this code if I’m using the Appearance>>Menus functionality of WP for my navigation menu and I want to add a link in that?

What file do I need to edit?

Comment by Eric says:

Brilliant and simple. Thank you very much !

Comment by mirliton says:

Hi,

How to exclude a concrete category of the random posts?

Thank you

Comment by JamieP says:

Great post – was very easy to follow and worked a treat. You mention in the post that if you want to add an actual graphic as opposed to just the text link, you will need to add the appropriate code…do you have a link for that by any chance?

Also, what would I need to add to make the random post from a specific category only?

Comment by Jimmyjames says:

This was exactly what I needed. Thank you kindly!

Comment by Briley Kenney says:

Hey Brandon, unfortunately for whatever reason I’ve found that a lot of PHP code doesn’t work in native WordPress widgets. In order to add the feature to your blog you need to edit the WordPress source files.

“The following code can be added anywhere you would like the link to appear:â€Â This means that the code can be placed anywhere on your blog providing you add it to the related source file.

For instance, I have a site menu located in the header of my personal blog. Naturally that was where I added my random post link, and because the menu is in my header I added it to my “header.php” file (in the root directory of my active WordPress theme).

If you’re looking to add the random posts feature into your sidebar your best bet is manually adding the code to your “Sidebar.php” file using the WordPress appearance editor.

The integrated html code editor can be found by navigating to “Appearance> Editor>”.

Hope this helps!

Comment by Brandon M. Sergent says:

A working example would be great or a more webdev-ignorant friendly explanation.

“The following code can be added anywhere you would like the link to appear:”

I pasted the link into a text widget on my blog and it didn’t work, I was basically treating it like an HTML.

Codeable info