0

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?

0

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;
  • That would actually work, thank you. But by using this approach I wouldn't be able to setup a pagination, as I'm not using the WP loop. – Quentin Veron Oct 16 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.