I have an array of ids in a specific order. I use post__in
in my query to get posts from this array. I have some duplicate post IDs that I need to show twice in the page but the query seems to ignore them.
Let’s say the array is as follows
array values = 1,2,3,4,2,5
When I pass this array to post__in
, it doesn’t take value '2'
twice as i specified in the array. Is there a workaround for this?
The situation
An array like
array( 1,2,3,4,2,5 )
will be read like the following:which seems ok. But when you look at the
WP_Query
object, you’ll see the following part added to the query string:This means that each post will be fetched once.
Conclusion
So unless you don’t modify the query to include the post multiple times, you’ll be left with only an option to do this on runtime.
Runtime configuration possibilities and x-raying the internals
Every basic loop looks like this:
Now the
global $post
refers to what gets setup viathe_post()
. It basically is a wrapper for$GLOBALS['wp_query']->the_post();
. And when you take a look at theWP_Query::the_post()
method, you’ll find quite interesting things:There you see
$this->next_post();
called. And this one looks from the inside like the following:Solution
So you see that the main thing the loop relies on, is the
current_post
counter. This one is as well accessible from the front end/a template. Simply useto “jump” between posts. So if you need to repeat a post after post X, then just check against it, switch for one round and then skip back.
You can loop your ids and call
get_post
andsetup_postdata
: