Well I can't figure this one out...
I have this Wordpress I use as a photo gallery blog.
I have a basic setup using the main default loop for posts.
Like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; ?>
<b>Not Found</b>
<?php endif; ?>
In the sidebar and where ever, I want to appear random posts.
I've managed to do that. With this:
<?php query_posts($query_string . 'showposts=1&orderby=rand'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; endif; ?>
It looks amazing! In theory.
There are duplicate posts all over the place. And that just looks stupid.
I have read lots of articles but I just can't seem to get it to work :(
Any help would be much appreciated.
After a good night of sleep, here's what I have done:
Creating array with post ID:
The Main loop where at the end I record the post ID to array:
And the random post code using post__not_in to avoid duplicates and again recording post ID:
Works evertime!
You can do amazing stuff with this :)
Thanks to paislee and Arvind Pal for helping out.
Skip would-be duplicates by remembering displayed ID's from the first loop
In your first loop, each time:
Change your random loop opening like this:
Due to randomness in the number of duplicates, you may want to alter your query so that it gets all posts, and change the second loop to
break
once the desired number of random posts is reached.Notes
showposts
is depracated. Replaceshowposts=1
withposts_per_page=-1