Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Mysql RAND() seed for random post pagination WordPress

  • SOLVED

I have 2 loops and I need to accomodation pagination for both, some how. Here's how the post order needs to display for archive pages ....

<strong>Homepage = random order w/ pagination</strong> (already working)
<strong>Yearly Archives = random order w/ pagination</strong> (already working)
<strong>Monthly Archives = random order w/ pagination</strong> (already working)
<strong>Daily Archives = <strong>post_order DESC w/ pagination</strong></strong> (not working)

Random posts don't paginate by default but I found [[LINK href="http://wordpress.stackexchange.com/questions/31647/is-it-possible-to-paginate-posts-correctly-that-are-random-ordered"]]this random order pagination code on WPSE[[/LINK]] and it work fine and as expected. This is the 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;
}


I don't really know <em>how</em> this code works, but it seems to set the randomisation on <em>all</em> archives. <strong>I need to retain this random post pagination on the Homepage, Year & Month archives (which all already work) but somehow using standard post_order descending for the Daily archives</strong>.


<strong>My standard loop is from _s theme ...</strong>

<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>

... do stuff here

<?php endwhile; ?>
<?php endif; ?>


<strong>My random loop is so ...</strong>

<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$lastposts = get_posts( $args );
// Some post-related data is not available to get_posts by default, such as post content through the_content(), or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument: http://codex.wordpress.org/Template_Tags/get_posts
foreach($lastposts as $post) : setup_postdata($post); ?>

... do stuff here

<?php endforeach; ?>

Answers (2)

2012-10-23

John Cotton answers:

Are you saying that you want to order posts randomly, but with pagination ie when you view page 2 five minutes later you don't get items that randomly appeared on page 1 five minutes earlier?

Because your current code will change the order of the posts every time you view them since it reseeds the random number generator.

What you would need to do is store the seed for each user (in a session variable or form field) so that your "randomness" is consistent across queries.


TheLoneCuber comments:

Well I'm hoping to implement Infinite Scroll after I get normal pagination working. So I'm hoping to keep readers on index.php and not have them visiting or revisiting any /page/[number] urls at all.

Would Infinite Scroll change anything in regards to the seeding?


John Cotton comments:

DBranes answer looks good...

2012-10-23

Dbranes answers:

hi, you could try to use is_day(), to check if you are viewing the daily archive:


session_start();
add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
if( !is_day()) ){
$seed = $_SESSION['seed'];
if (empty($seed)) {
$seed = rand();
$_SESSION['seed'] = $seed;
}
$orderby_statement = 'RAND('.$seed.')';
}
return $orderby_statement;
}


TheLoneCuber comments:

Doesn't seem to like that code at all?

HTTP Error 500


Dbranes comments:

ahh,

please change change this

if( !is_day()) ){


into
if( !is_day() ){

got an extra ) there ;-)


TheLoneCuber comments:

Works great. Thanks a lot, again.

Can you foresee any problems with Infinite Scroll and this pagination?


Dbranes comments:

just that there isn't any plugin you might install later that might use the posts_orderby filter.

You could then try to set the priority low (so your code will be the last to use the filter) fx:

add_filter('posts_orderby', 'edit_posts_orderby',99);

Sometimes there are problems with the writing permissions for the sessions, but since it is working for you now - it looks fine.