Order Random without duplicates on pages

2019-06-04 00:17发布

It's possible make loop with random without duplicate entries?

This is my code, but in page 2, repeat posts of page 1.

<?php global $query_string;

query_posts( $query_string . '&orderby=rand' ); ?>

<?php while(have_posts()): the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <div class="post-content">

标签: wordpress
1条回答
不美不萌又怎样
2楼-- · 2019-06-04 01:04

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

functions.php

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;
}
查看更多
登录 后发表回答