How To Redirect Website Users And Viewers To A Random Post On WordPress

[Normal_Box]Redirect WordPress Website Users and Visitors To A Random Post

How To Redirect Website Users To A Random Post

This is written for instances where you want to redirect your WordPress website users and viewers to a random post, via menu item, page, plain ol’ text link, button, etc using SEO friendly redirect codes.

  • For other random post scenarios read this post to use a simple PHP code.
  • [/Normal_Box]

    Step 1: Create a php file

    Creating a PHP file can be done many different ways. (Beginner examples: Use Microsoft Notepad and paste the code below, then save your extension as PHP. Or change an existent PHP file and paste the code below. )

    Copy and paste Name the file page-random.php and paste in the code below:[Normal_Box]

    // set arguments for get_posts()
     $args = array(
     'numberposts' => 1,
     'orderby' => 'rand'
     );
    // get a random post from the database
     $my_random_post = get_posts ( $args );
    // process the database request through a foreach loop
     foreach ( $my_random_post as $post ) {
     // redirect the user to the random post
     wp_redirect ( get_permalink ( $post->ID ), 307 );
     exit;
     }

    [/Normal_Box]

    Step 2: Upload page-random.php into your theme’s directory

    Your theme’s directory can be found in your FTP manager under wp-content>Themes>Your Theme.

    Step 3: Create a page titled “random” on WordPress

    Login to your WordPress dashboard, create a new page and title it random.

    Once you have created the random page in your WordPress interface users who visit http://yourwebsite.com/random will be directed to a random post.

    307 HTTP redirect codes

    Adapted into the code above is a 307 status code. Using 307 codes for redirecting users to random posts will allow search engine bots to understand this is just a temporary redirect, otherwise redirects are not considered SEO friendly.[/Normal_Box]
    [eps-product id=”2371100″]

    Sean Doyle

    Sean Doyle is a tech author and engineer with over 20 years of experience in cybersecurity, privacy, malware, Google Analytics, online marketing, and other topics. Sean's content has been featured in numerous publications.

    13 Responses

    1. Awesome, works great, I was looking for this code for ages.

    2. Thanks, that was easy!

    3. Christopher says:

      Others wrote bad redirect codes which I ended up getting penalized for.
      I now figure because they didn’t include the 307 in their redirect code part only a 302.
      Thank you for sharing, wish I found this first.

    4. Good post. Always works like a charm.

      This is the right way to redirect users.

      You can also change the number of posts from 1:
      ‘numberposts’ => 10,

    5. Brian says:

      Thanks for including the 307 redirect code.

    Leave a Reply

    Your email address will not be published.

    This site uses Akismet to reduce spam. Learn how your comment data is processed.