WordPress: Create A Random Post Button

A good way to keep visitors on your website and reduce bounce rate is to use a “random post” button. This button can be used anywhere on your website (you can see ours in the sidebar), and when clicked it will take the visitor to a completely random post.

This is actually very easy to achieve, you simply add the following code into your theme’s function.php file:

function random_post() {
       global $wp;
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');

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);

The create a link and put it in the desired location of your website. The link should look something like the following…

<a href="/?random=1">Random Post</a>
Free Graphics, Icons, Tutorials
Free Graphics Free Christmas Vector Icon Graphics Pack 2017Free Fitness Vector Icons Graphics PackFree Camping Vector Graphics PackFree Summer Graphics PackFree File Icon PackFree Fast Food Vector Graphics
Sharing is caring...
Like & Follow
Share the love, share this page! Facebook Twitter Digg Reddit LinkedIn Pinterest Email
Close [X]
The Web Taylor
1000 Lakeside North Harbour Portsmouth, Hampshire PO6 3EN
02392 123358