• Hey all,

    I’ve searched this one quite a bit, and can’t quite find a resolved answer!

    Basically, I want to create a custom loop where I can set the post order to be random and the number of displayed posts to 10, for example. I then need the pagination to retain the original random post order from page to page.

    At the moment, each new page of 10 posts is randomising itself again, so you get duplicates or posts that are unlucky enough to never show!

    I did find this post, where they are able to do this, by a small addition to functions.php:

    http://wordpress.stackexchange.com/questions/31647/is-it-possible-to-paginate-posts-correctly-that-are-random-ordered

    This does seem to work for me, but the problem is, the answer given only ever offered a solution to using it on your main loop, which then overwrites all my default loops in the theme. I only want to use it on one custom loop!

    I’m just not up to the task of changing this small inclusion to the functions file so that it can just be called in a custom loop. Can anyone help?!

    Here is the code that can be added to functions.php to modify the main loop:

    session_start();
    
    add_filter('posts_orderby', 'edit_posts_orderby');
    
    function edit_posts_orderby($orderby_statement) {
    
        $seed = $_SESSION['seed'];
        if (empty($seed)) {
          $seed = rand();
          $_SESSION['seed'] = $seed;
        }
    
        $orderby_statement = 'RAND('.$seed.')';
        return $orderby_statement;
    }

    Thanks in advance for any help!

Viewing 3 replies - 16 through 18 (of 18 total)
  • Thank you @lancemonotone.

    So, this is how i used your function and it works perfectly :

    session_start();
    add_filter('posts_orderby', 'myrandom');
    function myrandom($orderby_statement) {
        if(!is_admin()){
            switch ($_GET['order']) {
                case "random":
                    if(is_home() && !wp_get_referer()) unset($_SESSION['seed']);
                    $seed = $_SESSION['seed'];
                    if (empty($seed)) {
                        $seed = rand();
                                $_SESSION['seed'] = $seed;
                    }
                    $orderby_statement = 'RAND('.$seed.')';
                default:
                    break;
            }   
    
        }
        return $orderby_statement;
    }

    There’s a logic bug in the function which will prevent the random seed from being reset if the page is home or if the referrer is not within the site. Updated code to come.

    /**
     * Randomize posts, keeping same order on subsequent Home page and single post views
     * if referer is within the site.  Home page will randomize again if reloaded.
     *
     * @global $_GET['order'] default or 'random' sets random, 'date' sets date.
     *
     * @param string $orderby_statement
     * @return string Modified orderby statement
     */
    function edit_posts_orderby($orderby_statement) {
    	if(!is_admin()){
    		switch ($_GET['order']) {
    			case "date":
    				break;
    			case "random":
    			default:
    				if(!isset($_SESSION['seed'])) {
    					echo 'New Grid';
    					$_SESSION['new_request'] = true;
    				} else {
    					// $refesh_flag will be true if $request_signatures match between page loads.
    					$request_signature = md5($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'].implode('',$_POST));
    					// $interior_flag will be true if the last page URL contains the base url but is not the base url.
    					$interior_flag = is_int(strpos(wp_get_referer(),home_url())) && home_url() != wp_get_referer();
    					// if we are home AND the page has been refreshed AND the last page was not an interior page,
    					// unset the seed and flag as a new request so the grid will appear the same upon return.
    					if(is_home() && $_SESSION['last_request'] == $request_signature && $interior_flag == true){
    					//echo 'True';
    						unset($_SESSION['seed']);
    						$_SESSION['new_request'] = true;
    					} else {
    								//echo 'False';
    						$_SESSION['new_request'] = false;
    						$_SESSION['last_request'] = $request_signature;
    					}
    						}
    				$seed = $_SESSION['seed'];
    				if (empty($seed)) {
    					$seed = rand();
    					$_SESSION['seed'] = $seed;
    				}
    				$orderby_statement = 'RAND('.$seed.')';
    		}
    	}
    	return $orderby_statement;
    }
Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘Random Post order not working with pagination’ is closed to new replies.