Forums Forums Search & Filter Pro Random is not Random

Viewing 4 posts - 1 through 4 (of 4 total)
  • Anonymous
    #9101

    It looks like it’s actually sorting results by date instead. Is this a bug?

    Ross Moderator
    #9153

    Hey Robbin,

    We use the random option as defined by WP – http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    I suspect you may have some custom code or another plugin that is interfering with the search results? – anything that modifies queries or uses the pre_get_posts hook could do this.

    If you choose another option, other than random, do you find that the results are still ordered the same?

    Thanks

    Anonymous
    #9258

    Thanks for your reply.

    All of our other sort options are displaying in the correct order and the random order is working when set as the default display.

    However, when set as a sorting option it displays a in the same order as descending by date.

    I should mention that to get the random by default setting to work, I had to add this to the functions.php file:

    session_start();
    
    add_filter( 'posts_orderby', 'randomise_with_pagination' );
    function randomise_with_pagination( $orderby ) {
    
    	if( is_front_page() || is_page(5020) ) { // Page 5020 is the page the form is on. 
    
    	  	// Reset seed on load of initial archive page
    		if( ! get_query_var( 'paged' ) || get_query_var( 'paged' ) == 0 || get_query_var( 'paged' ) == 1 ) {
    			if( isset( $_SESSION['seed'] ) ) {
    				unset( $_SESSION['seed'] );
    			}
    		}
    	
    		// Get seed from session variable if it exists
    		$seed = false;
    		if( isset( $_SESSION['seed'] ) ) {
    			$seed = $_SESSION['seed'];
    		}
    	
    	    	// Set new seed if none exists
    	    	if ( ! $seed ) {
    	      		$seed = rand();
    	      		$_SESSION['seed'] = $seed;
    	    	}
    	
    	    	// Update ORDER BY clause to use seed
    	    	$orderby = 'RAND(' . $seed . ')';
    	}
    
    	return $orderby;
    }
    Ross Moderator
    #9962

    Hey Robbin, I investigated this further and there does indeed appear to be a bug with this – I’ll include a fix in the next update.

    Thanks

Viewing 4 posts - 1 through 4 (of 4 total)