Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Create a Page that Displays a Random Post in WordPress

Last updated on by
Follow WPBeginner on YouTube
How to Create a Page that Displays a Random Post in WordPress

We mentioned a plugin that allows you to have Stumbleupon like Browsing system in WordPress. The problem with that plugin is that it redirects a user to the actual URL which can create some user end issues such as page not loading, redirect error etc. In this post we will show you how you can create a page that displays a random post in WordPress, so you can simply send your user to that page and they can read a new post every time they refresh the page.

EDIT (11/29/2012): We are no longer using this technique because it prevented us from showing proper social buttons. Instead we have a new method that allows you to redirect the user to a random post using /random/. This method is much better. Read more about How to redirect users to a random post in WordPress.

Step 1: of this tutorial is to create a custom page template in WordPress.

Step 2: Add the following code in your custom page template:

<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>

<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>

<?php the_content(); ?>

<?php endwhile;
endif; ?>

That’s it. Depending on the name that you choose for the page, you can now create a button in your navbar for users to click on that page and view a random post.

Live Example

(Simply refresh a page to see a new post)


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • morgan

    Hi,

    I’ve tried to use this method but I just noted several problems:
    - as the link is always blog/random ,all social links (facebook share, etc) are linked to /random, and not to the article
    - comments are not displayed…

    I noticed that the random method on wpbeginner is not the one displayed aboved, as when you enter http://www.wpbeginner.com/random, you are redirected to the post URL…what is method used?

  • http://www.zen-moments.com John Rocheleau

    Solution:

    There wasn’t an extra closing tag for a div as indicated in validation. The openings and closings all equaled out.

    The validation check generated that error notice due to a previous issue in the page with a call for an Adsense ad block. I had commented it out –perhaps not completely — because it was still generating a closing tag for a script that was not open to begin with. That caused the validation to erroneously indicate the extra div closing.

    It took a while to track it back, but it all works fine now in any browser after deleting that commented out Adsense call line line (didn’t need it anyway). The page validates much better now with no div error remaining.

    Thanks again for your help. You steered me in the right direction.

    :-)
    John

  • http://www.zen-moments.com John Rocheleau

    Thank you for checking that out. I thought I went over that template page carefully, but I guess not. I assume you meant the extra div being closed prematurely is on the template page I created. Must run out for the day but will go over that when I return.

    Thanks again :-)

  • http://www.zen-moments.com John Rocheleau

    Hi,

    I have followed instructions here and my random page works fine in FireFox, but the css is not rendering properly at all in IE or Chrome. Oddly enough it does render ok in Chrome on my local version (local server) but not on the live version.

    You can test it out for yourself if you go to my site and click on the Random Moments Icon in the sidebar or the menu item.

    Any thoughts?

    • http://www.wpbeginner.com Editorial Staff

      There is an error in coding.

      You have an extra div being closed before it is supposed to be:

      Check out the Validation