1

most people appear to have a problem removing "duplicate posts", I've been trying to retrieve a pre-defined list of posts via 'post__in' and "p" array. The array contains certain post_ids twice, on purpose, like

array (164,189,200,164,12,13,164)

However, the result set only contains the posts once.

I would, for efficiency reasons, prefer to avoid to create a new loop for all elements in the array. Is there a way to achieve this? I can't seem to get it done.

Thanks!

3
  • post__id, like 'post__in' => array( 2, 5, 12, 14, 20 ), work with an arrayy of post IDs. Maybe you add your code here to helps you.
    – bueltge
    Feb 28, 2018 at 8:31
  • Hey! Thanks! That's interesting - I can't find "post__id" in the documentation for wp_query (codex.wordpress.org/Class_Reference/…). However, it doesn't appear to work in my case, using post__id as an argument as shown below results in a result set of all posts of the post_type, not just three times 163, while post__in will give me the post 163, but only once $new_query = new WP_Query( array ( 'post__id' => array(163,163,163), 'post_type' => 'my_post_type' ));
    – user137681
    Feb 28, 2018 at 15:42
  • Im sorry, post__id was a mistake, post__in is the right key.
    – bueltge
    Mar 1, 2018 at 8:44

1 Answer 1

1

You can achieve your desired result by adding this code.

<?php 

global $post;
foreach ($p_ids as $p_id) :
    $posts = get_post($p_id);
    setup_postdata( $posts );
    the_title();
endforeach;
3
  • "I would, for efficiency reasons, prefer to avoid to create a new loop for all elements in the array." Feb 28, 2018 at 8:19
  • Thanks! Yes, I've done this and it works, but if possible I'd prefer a one query solution.
    – user137681
    Feb 28, 2018 at 15:43
  • "Is there a way to include a post multiple times in the same query result?" - Yes. It turns out / this is one of the ways. Feb 7 at 8:29

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.