• Resolved oriver

    (@oriver)


    Hi,

    I’m trying to get my posts to display randomly with infinite pagination, and within their category. I’ve tried the following code:

    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;
    }

    Which displayed posts in a “random” order within their category, or for the homepage all posts – but it was the same random order.

    For example, when activating this code it displays the posts for say the gardening category as: B, A, D, C. And no matter if you refresh or open a new tab, it will always be in this order. While for the category books it might display the posts as: X, U, W, Z, but again will stick with this “random” order for its category.

    Does anyone have any ideas on what I can try? Other codes I’ve given a go seem to duplicate posts, or can’t recognise categories. This is the closest I’ve got so far, but obviously a locked random order is no longer random if it stays the same.

    Thank you in advance for any help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Passing a seed via session var ensures the same ordering will be used until the session is ended or the seed changed. Usually closing the browser will kill the session, but reloading or new tabs will not.

    To ensure the same ordering is used for infinite scroll, but it changes on reload, instead of saving it in a session var, it should be passed to the infinite scroll script, which in turn will be passed back to the server when the script wants more data. The orderby filter then picks up the passed seed from $_GET instead of getting it from the session var.

    Thread Starter oriver

    (@oriver)

    Hi @bcworkz

    Thank you for helping.

    You’re right when I close the browser that’s when it refreshes the random order.

    I’m afraid I’m not really a developer, just trying to learn as I go. Could you please explain what you mean by passing the code to the infinite scroll script? I’m currently using code snippets to insert the code.

    Thank you again for your help.

    Moderator bcworkz

    (@bcworkz)

    If the infinite scroll script is not yours, altering it might be a challenge. But there’s a reasonable chance you could. Client side WP scripts are enqueued in PHP. When/where that is done, you can also pass JS inline vars from PHP to JS using wp_localize_script(). Pass the seed used in the posts query order by clause that way.

    In the script, where it requests more post data, in the request URL, include this seed value in the request URL as a query string. WP doesn’t know about this kind of query var, so you need to whitelist it through the “query_vars” filter. Once whitelisted, you can get the seed value from the query object’s query vars. You no longer need to get it from $_GET. Use the passed query var in your order by filter.

    Initializing the seed is a little tricky because you need it in two different callbacks. It’s hard to know which comes first. It also needs to be communicated to the other callback. A global value is the simplest way, even though its use is generally discouraged. If it’s not clear which comes first, you could conditionally initialize it in both places if it does not already exist in either the global or in the query vars.

    Thread Starter oriver

    (@oriver)

    @bcworkz

    Thank you, I managed to grasp a rough understanding.

    Think it will be beyond me right now, but when I learn a bit more this will really help, thank you.

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

The topic ‘Random Pagination within Categories’ is closed to new replies.