Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8146763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:06:44+00:00 2026-06-06T14:06:44+00:00

I am using this function to redirect to a random post. I am trying

  • 0

I am using this function to redirect to a random post. I am trying to find a mechanism to prevent the same random post to appear twice in a row. Can anyone give a hint?

function sofa_view_random_post() {

    // make array of all posts IDs
    $q = get_posts('numberposts=-1');
    $array = array();
    foreach($q as $p) {
        $array[] = $p->ID;
    }

    // randomize array to get random post
    $k = array_rand($array);
    $v = $array[$k];
    wp_redirect( get_permalink( $v ) ); exit;

}

I get all posts IDs first and put them to array. I randomize the array to get a value. But not sure how to prevent same value from being triggered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:06:46+00:00Added an answer on June 6, 2026 at 2:06 pm

    Store the previous result in the session, and make sure you don’t pick that one again:

    if( !isset( $_SESSION['last_random_post'])) {
        $_SESSION['last_random_post'] = -1;
    }
    
    do {
        $k = array_rand( $array);
    } while( $k == $_SESSION['last_random_post']);
    
    $_SESSION['last_random_post'] = $k;
    

    I’m not sure how this applies to wordpress specifically, but it should be easily adaptable:

    For completeness, here is the function incorporating the above code:

    function sofa_view_random_post() {
    
        if( !isset( $_SESSION['last_random_post'])) {
            $_SESSION['last_random_post'] = -1; // Or null, as long as its an invalid ID
        }
    
        // make array of all posts IDs
        $q = get_posts('numberposts=-1');
        $array = array();
        foreach($q as $p) {
            $array[] = $p->ID;
        }
    
        // randomize array to get random post
        do {
            $k = array_rand( $array);
        } while( $k == $_SESSION['last_random_post']);
    
        $_SESSION['last_random_post'] = $k;
    
        $v = $array[$k];
        wp_redirect( get_permalink( $v ) ); exit;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get selected text of Iframe using Rangy using this function;
I'm trying to download users profile picture from facebook in PHP using this function
I am using this function to play sound on my iPhone app. the problem
I am using this function for my cURL queries. function get_source($url){ $ch = curl_init();
I'm using this function to convert CamelCase to dashed string: function camel2dashed($className) { return
I am using this function to close existing form and open a new form.
I'm using this function to extract files from .zip archive and store it on
I am using this function https://www.php.net/sys_getloadavg to get a hint about my server load.
Dear all i am using this function to get the GMT time, i am
I'm using this function: preg_match_all('/((\d{9})\.html)/s', $content, $results); It works fine but i just want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.