• Hi – I have been working on a site that needs the category pages to randomize with correct pagination. I altered some code I found on the web. It worked when I put in 2 categories – but when I added them all in – it is no longer working. What did I miss? Should I use the slug instead? I am on new meds right now so I must have missed something….

    add_filter( 'posts_orderby', 'randomise_with_pagination' );
    function randomise_with_pagination( $orderby ) {
    	if( is_category( 'Alabama, Arizona, Alaska, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hamshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, Washington DC, West Virginia, Wisconsin, Wyoming' )  ) {
    	  	// 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;
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    use pre_get_posts instead to set the order to random. Add a test to make sure you’re on a category archive page.

    https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    Note that you’ll need to disable caching for those pages if you actually want visitors to see the random ordering. ??

    per https://developer.www.ads-software.com/reference/functions/is_category/#user-contributed-notes (you need to expand the example code):

    this line:
    if( is_category( 'Alabama, Arizona, Alaska, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hamshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, Washington DC, West Virginia, Wisconsin, Wyoming' ) ) {
    needs to be changed to:
    if( is_category( array('Alabama', 'Arizona', 'Alaska', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', ETC-YOU-GET-THE-IDEA, 'Wisconsin', 'Wyoming') ) ) {

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category Page Random post order with correct pagination’ is closed to new replies.