greg9885
Member
Posted 3 years ago #
As stated in the title, I am looking for a way to be able to sort all of my posts within my site randomly. I want to keep the way that they are sorted now (which is just the way wordpress sorts them by default) but ALSO add a way so that users can sort the posts randomly.
I'm pretty sure that this can't (and shouldn't) be done on a "page template." So I'm wondering if I should be using some sort of query_posts option along with sortby rand?
Any help with this would be greatly appreciated, thanks in advance.
greg9885
Member
Posted 3 years ago #
Just a friendly *bump* to try and get my issue resolved. I do need some help though please.......??
This thread and this documentation page looked promising.
p/s: The first link is a little dated so it might not need all the changes suggested to achieve the same effect.
greg9885
Member
Posted 3 years ago #
That only sorts 5 random posts, I need an option to sort all of the posts in my site randomly. Any ideas?
@greg9885: Maybe I'm misunderstanding what you're asking for here: I'm working on the fact that you want to display a list of random posts. Is this right?
Just a quick modification *should* display the posts randomly (not tested):
<?php query_posts('orderby=rand'); ?>
Although having had a look through some of the variables, it could be that you could potentially add &posts_per_page=-1 after rand to ensure all posts are displayed.
greg9885
Member
Posted 3 years ago #
I believe I tried just using only 'orderby=rand' before, but it I don't think it worked.
Also, should I be putting this code on a page template? I want the user to click an option in the top navigation to sort...
greg9885
Member
Posted 3 years ago #
One more thing... I've heard that the pagination doesn't work correctly (or maybe not at all?? I think??) when you use query posts. Anyone know if there is any truth to this?
I decided to test out the example on the documentation page, and it does work for me (using get_posts instead) and it uses foreach, which I think is necessary for query_posts as well, so apologies there for my mistake. Anyway, this is what is placed into e.g. a page-template (or can go into a category template as well):
<ul><li><h2>A random selection of my writing</h2>
<ul>
<?php
$rand_posts = get_posts('numberposts=5&orderby=RAND()');
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</li></ul>
I took out numberposts=5& and it seems to be showing everything in a random order.
greg9885
Member
Posted 3 years ago #
Ok, yes this does work... but where would I put this and in which template. I'm almost positive I can't use the page template because it doesn't allow pagination. Do I have to write some sort of conditional statement?
greg9885
Member
Posted 3 years ago #
If you take out numberposts=5& it will show all the posts, so there wouldn't be any pagination. However, this post (using a slightly different method to get_posts /query_posts) seems to resolve the issue?
greg9885
Member
Posted 3 years ago #
Whew... Almost there. I've run into one last snag though, the random post sort is (for example) displaying posts that were on page 1, on page 2.
To try to explain this more: The site doesn't (for lack of better words) "remember" which posts are on which page.
Here is the code that I have so far:
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('orderby=rand&showposts=' . $limit=5 . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
Not quite sure how best to get round that at the moment I'm afraid, but I guess it proves that this method really does deliver random posts!
greg9885
Member
Posted 3 years ago #
hmmmm... I still can't quite figure it out :( Anyone have any ideas how I can fix this?