title: Get random post in Wordpress
body: <p>How do I get a random post in Wordpress?</p>
  
  <p>I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
  I tried searching for a code on Google and here at stackoverflow but no success.</p>
  
  <p>Thanks...</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here is my template code:</p>
  
  <pre><code>&lt;?php /*Template Name: Random*/ ?&gt;
  
  &lt;?php get_header(); ?&gt;
  
  &lt;nav&gt;&lt;?php wp_nav_menu(array('menu' =&gt; 'Main Nav Menu')); ?&gt;&lt;/nav&gt;
  
  &lt;div id="main-content-archive"&gt;
  
  &lt;div class="grey-text"&gt;Random post&lt;/div&gt;
  
          &lt;?php $query = new WP_Query( array ( 'orderby' =&gt; 'rand', 'posts_per_page' =&gt; '1' ) );?&gt;
  
          &lt;?php if (have_posts()) : while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();
          echo '&lt;li&gt;';
          the_title();
          echo '&lt;/li&gt;';
          ?&gt;
  
  &lt;?php endwhile; ?&gt;
  
  &lt;?php else : ?&gt;
  
      &lt;h2&gt;Not Found&lt;/h2&gt;
  
  &lt;?php endif; ?&gt; 
  
  &lt;/div&gt;
  &lt;?php get_sidebar(); ?&gt;
  &lt;?php get_footer(); ?&gt;
  </code></pre>