I have an arrays of IDs which may contain duplicate values.
Array
(
[0] => 24
[1] => 11
[2] => 60
[3] => 11
)
I'd like to loop through those IDs using WP Query and the post__in
property.
Array
(
'post__in' => $posts,
'post_type' => 'any,
'orderby' => 'post__in'
)
Everything work as expected, but duplicate IDs are removed by default.
Is there any way to prevent it?
I have an arrays of IDs which may contain duplicate values.
Array
(
[0] => 24
[1] => 11
[2] => 60
[3] => 11
)
I'd like to loop through those IDs using WP Query and the post__in
property.
Array
(
'post__in' => $posts,
'post_type' => 'any,
'orderby' => 'post__in'
)
Everything work as expected, but duplicate IDs are removed by default.
Is there any way to prevent it?
It's a bit less efficient, but if you have an array of IDs, possibly including duplicates, and want to get the posts for each, I'd suggest use get_post()
on each ID, giving you an array of posts.
$post_ids = [ 24, 11, 60, 11 ];
$posts = array_map( 'get_post', $post_ids );
global $post;
foreach ( $posts as $post ) : setup_postdata( $post );
// the_title(); etc.
endforeach;