wp query - Avoid removing duplicate posts

admin2025-04-19  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?

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?

Share Improve this question asked Oct 15, 2019 at 20:21 QuentinQuentin 158 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745078113a283689.html

最新回复(0)