pagination - Is it possible to paginate posts correctly that are random ordered?

I found this problem on Wordpress Support and the topic unfortunately is now closed.I have this same issue... (read be

I found this problem on Wordpress Support and the topic unfortunately is now closed. I have this same issue... (read below)


We have created a site where members can recommend things like favourite books, movie, songs, etc. For this problem I will use the Movies page as an example.

The "Movies" page is ultimately a custom page template that asks wordpress to display a random list of ALL posts that have been given the category "movies" (category 31). It displays the title of these movies in random order using the code below.

<?php 
$rand = new WP_Query("cat=31&showposts=-1&orderby=rand"); 
while($rand->have_posts()) : $rand->the_post();
?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php endwhile; ?>

Problem is the list is getting rather long and I would like to split it up into two or more pages of about 10 movies each. To achieve this, I have used the code below.

<?php 
$page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
query_posts("cat=31&orderby=rand&showposts=10&paged=$page"); 
while ( have_posts() ) : the_post() 
?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php endwhile; ?>

But there is a problem because although it splits up the data into pages of 10 posts each (paginates), it fails to include a new set of 10 posts on page 2 and so forth. In other words, because it is listing things in a random order, it just goes out and gets another 10 random posts (or in this case movie titles). As a result, we have some repeated movie title posts instead of a new set of 10 random movie titles on page 2, etc.

My question is - what can I add to this code to get wordpress to "remember" which random 10 posts it included on page 1, and then have it get a new set of 10 posts to put on pages 2, 3, etc. until all posts are displayed. I would like there to be only one occurrence of a post per page when sorted randomly in sets of 10.

I found this problem on Wordpress Support and the topic unfortunately is now closed. I have this same issue... (read below)


We have created a site where members can recommend things like favourite books, movie, songs, etc. For this problem I will use the Movies page as an example.

The "Movies" page is ultimately a custom page template that asks wordpress to display a random list of ALL posts that have been given the category "movies" (category 31). It displays the title of these movies in random order using the code below.

<?php 
$rand = new WP_Query("cat=31&showposts=-1&orderby=rand"); 
while($rand->have_posts()) : $rand->the_post();
?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php endwhile; ?>

Problem is the list is getting rather long and I would like to split it up into two or more pages of about 10 movies each. To achieve this, I have used the code below.

<?php 
$page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
query_posts("cat=31&orderby=rand&showposts=10&paged=$page"); 
while ( have_posts() ) : the_post() 
?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php endwhile; ?>

But there is a problem because although it splits up the data into pages of 10 posts each (paginates), it fails to include a new set of 10 posts on page 2 and so forth. In other words, because it is listing things in a random order, it just goes out and gets another 10 random posts (or in this case movie titles). As a result, we have some repeated movie title posts instead of a new set of 10 random movie titles on page 2, etc.

My question is - what can I add to this code to get wordpress to "remember" which random 10 posts it included on page 1, and then have it get a new set of 10 posts to put on pages 2, 3, etc. until all posts are displayed. I would like there to be only one occurrence of a post per page when sorted randomly in sets of 10.

Share Improve this question edited Oct 21, 2011 at 11:52 Rarst 100k10 gold badges161 silver badges298 bronze badges asked Oct 20, 2011 at 18:59 user9604user9604 3211 gold badge3 silver badges4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 41

You can use a filter to modify the ORDER BY statement of WP_query.

That way you can manually set the query to use ORDER BY RAND($seed);

Mysql RAND() accepts a seed as an optional argument. Using a seed, it will return the same randomized result set each time.

So you can generate a random number on the first page load then store it in a SESSION variable and use that as the $seed for further paginated requests.

If you are trying to acheive this on your main loop for instance, you could add the following to your functions.php file.

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

Since the last versions of WordPress, you can now add a seed to the value of the orderby parameter of WP_Query:

$query = new WP_Query([
    'orderby' => 'RAND(' . $seed . ')',
    ...
]);

$seed is a random number. You should store it as a PHP session variable. Do not forgot to enable PHP session in WordPress by calling session_start() in you functions.php:

if (!session_id()) {
    session_start();
}

With this syntax you don't need to use the posts_orderby filter. Moreover, you don't have to make sure that the filter is only applied to the targeted WP_Query.

For further information please read this ticket on WordPress Core.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744754562a4591802.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信