Random Post order not working with pagination
-
Hey all,
I’ve searched this one quite a bit, and can’t quite find a resolved answer!
Basically, I want to create a custom loop where I can set the post order to be random and the number of displayed posts to 10, for example. I then need the pagination to retain the original random post order from page to page.
At the moment, each new page of 10 posts is randomising itself again, so you get duplicates or posts that are unlucky enough to never show!
I did find this post, where they are able to do this, by a small addition to functions.php:
This does seem to work for me, but the problem is, the answer given only ever offered a solution to using it on your main loop, which then overwrites all my default loops in the theme. I only want to use it on one custom loop!
I’m just not up to the task of changing this small inclusion to the functions file so that it can just be called in a custom loop. Can anyone help?!
Here is the code that can be added to functions.php to modify the main loop:
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; }
Thanks in advance for any help!
- The topic ‘Random Post order not working with pagination’ is closed to new replies.