Get random post in WordPress

How do I get a random post in WordPress?

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.

Read More

Thanks…

UPDATE:

Here is my template code:

<?php /*Template Name: Random*/ ?>

<?php get_header(); ?>

<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>

<div id="main-content-archive">

<div class="grey-text">Random post</div>

        <?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>

        <?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<li>';
        the_title();
        echo '</li>';
        ?>

<?php endwhile; ?>

<?php else : ?>

    <h2>Not Found</h2>

<?php endif; ?> 

</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Related posts

Leave a Reply

5 comments

  1. create a page template, and use the following code to get a random post:

    //Create WordPress Query with 'orderby' set to 'rand' (Random)
    $the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
    // output the random post
    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    

    then in a page, just use:

    <a href="the link to the page">see a random post</a>
    
  2. I found this post which gave me desired results…

    Here’s a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.

    Just add the following code to the functions.php file:

    add_action('init','random_add_rewrite');
    function random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
    }
    
    add_action('template_redirect','random_template');
    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);
               }
               wp_redirect($link,307);
               exit;
       }
    }
    

    Use mydomain.com/random/ as your href for your button that leads to the random post.

    Thanks everyone who contributed for your help…

    Cheers!

  3. I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it’s really easy, for a blog at

    http://mygroovywpsite.me/
    

    All you need to do is append it with ?random

    http://mygroovywpsite.me/?random
    

    I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.

    First make a Page in your site called “Random” / with the slug “random” — it does not need any content in it

    Then create a page-random.php template

    <?php
    /*
    Random Post Picker
    Use on page to send viewer to random post optionally mod query
    */
    
    // set arguments for WP_Query on published posts to get 1 at random
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'orderby' => 'rand'
    );
    
    // It's time! Go someplace random
    $my_random_post = new WP_Query ( $args );
    
    while ( $my_random_post->have_posts () ) {
      $my_random_post->the_post ();
    
      // redirect to the random post
      wp_redirect ( get_permalink () );
      exit;
    }
    ?>
    

    Then you get the re-direct for any link on your blog …../random w/o any wrestling with .htaccess

    I’ve done it this way because I’ve had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.

    I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()

  4. Another Simple solution to display Random Post

    1.First a create a custom page template. Name it as random post or a name of your choice!

    2.Open the page and remove the default wp loop and Paste the code below

    3.To change the no of post change the number ‘1’ to your choice!

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

    source: http://www.yengkokpam.com/displays-random-posts-in-a-page/

  5. Check This

    <ul>
    <?php
    $args = array( 'numberposts' => 5, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
       <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>