I am using multiple loops to grab certain categories ordered by RAND. The problem is when I put things in more than 1 category there can be duplicate results across the loops.
Is there a way to stop this from happening or not?
I am using multiple loops to grab certain categories ordered by RAND. The problem is when I put things in more than 1 category there can be duplicate results across the loops.
Is there a way to stop this from happening or not?
Possibly, you could grab the ids in the first query and add them to the post__not_in argument of the second?
http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
[code moderated - please follow the forum guidelines for posting code]
So how would this work with this code?
how to avoid duplicates:
http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action
Hi there. I am doing this, but it is still not working, any help?
I think maybe because this is assuming you're getting the posts from the same category whereas I need to get them from two seperate ones, but were a post is in both, make sure only it is displayed once from one category?
Ok I've managed to get it working, but occasionally my second loop only gets 2 posts instead of 3, any ideas?
that does happen with the 'continue' method, because the code grabs 3 posts and then 'jumps over' any duplicates;
there is the other method (also described in here http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action ) which is using 'post__not_in';
see:
http://pastebin.com/fjSZB9aQ
(btw: make sure to use the category slug (not the category title) with the 'category_name' parameter http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters )
Thankyou that worked perfectly and I've changed to slugs (:
You must log in to post.