0

This question already has an answer here:

I need to display in random order posts in multiple categories that have multiple pages. Example - 100 posts in an Automotive category with only 20 posts displayed per category page - the order is random but does not duplicate across pages.

Right now I have this code below but it is not working - it keeps randomizing on each page which causes people to see duplicate posts.

It would be better if we could have a way to tell it to reorder - much like Turboseek or Powerseek

add_filter( 'posts_orderby', 'randomise_with_pagination' );
function randomise_with_pagination( $orderby ) {
    if( is_category( 'art, asseen, auto, baby, business, cstore, candles, 
cell-phones, closeouts, clothing, collectibles, cosmetics, crafts, 
customer-returns, dollar-store, dvd-video, electronics, fashion-accessories, 
flags, food-grocery, furniture, general-merchandise, gifts, handbags, 
health-beauty, holiday-seasonal, housewares, incense, jewelry, knives, lawn-
garden, leather, licensed, logistics, made-in-usa, military, music, 
novelties, party-greeting-cards, patriotic-items, perfumes, pet-supplies, 
professional-supplies, promotional items, religious, security defense, 
shoes, smoking-products, socks-hosiery, sporting-goods, store-supplies, 
sunglasses, tools-hardware, toys-games, trade-shows, uncategorized, vaping, 
watches' )  ) {
        // 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;
 }

My question was identified as already answered but it is different because the code is not working across pages - I must have done something wrong.

marked as duplicate by Mark Kaplun, EAMann Feb 16 '18 at 0:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

If you're seeing those posts duplicate across multiple pages in a category, it would seem that your seed may be resetting. I would be interested to know what the output of get_query_var( 'paged' ) is on the category pages you're visiting.

As an alternative you may be able to store the seed in a cookie and access it on every page load.

Sudo code snippet:

$cookie_name = "myseed"
if(!isset($_COOKIE[$cookie_name])) {
    $seed = rand();
    setcookie($cookie_name, $seed);
} else {
    $seed = $_COOKIE[$cookie_name];
}

// Update ORDER BY clause to use seed
$orderby = 'RAND(' . $seed . ')';
  • This would completely replace "Get Seed from session variable" ...correct? To see how it is working I will give a link - mind you much more work is need to get the page to look right - I do not have pagination set up so you will have to put in /page/2 etc to get to the next page... will that make a difference? getthatwholesale.com/wordpress/category/auto – Denise Feb 2 '18 at 15:53
  • Yes, you would replace everything inside the if( is_category... statement. No, the seed should not change once set so it shouldn't matter if you haven't implemented links to the paginated pages. – Nate Gay Feb 2 '18 at 16:18
  • I used the code you provided but I am getting an error on – Denise Feb 2 '18 at 19:41
  • I keep getting an error in the area that has ** on either side of the code. Also when I edited my question - it would not take my code block for the upper part - unsure why. – Denise Feb 2 '18 at 19:49
  • Error message: Parse error: syntax error, unexpected T_IF in /var/www/vhosts/getthatwholesale.com/httpdocs/wordpress/wp-content/themes/gtw-child/functions.php on line 48 That is where the code enclosed in ** is (it is NOT enclosed in ** on the functions page just to be clear – Denise Feb 2 '18 at 19:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.