0

we added a custom query to our template which works fine. So we want to display all posts out of our array. But if we have some duplicate IDs in our array, WP ignores the duplicated IDs and shows every Posts->ID only once.

$args = array(
'post_type' => "custom_posttyp",
'order_by' => 'post__in',
'post__in'  => $post_ids, // array with (208, 212, 218, 208, 212, 218)
);

So based on this example the while-func just renders 3 posts instead of 6 - we think because of duplicated IDs - but we sometimes have to render also 2 or 3 copies of one post if the array has IDs twice, three times etc.

Any Idea? Ben

3
  • You're running a function to request data from the database, that data exists once and you can obviously not just tell it to gather an array containing the same data multiple times, that's not how it works. If you need to output the same data twice just run your loop twice, although I can't think of a single reason why you would ever need to?
    – Jebble
    May 18, 2017 at 11:32
  • this would be mad - we are using custom post types to display a selection of different posts selected my a frontend-user... so therefore we want to display all posts rather they are included twice or six, seven or twenty times... the output is important to show the right selection of a user
    – Ben
    May 18, 2017 at 11:41
  • "We want to display all posts" then request all posts. You're still not explaining why you would want to show identical posts multiple times from 1 query?
    – Jebble
    May 18, 2017 at 11:44

1 Answer 1

1

You can iterate over the array of IDs and look for the corresponding post in your results to generate output with duplicate posts:

$post_ids = array( 208, 212, 218, 208, 212, 218 );

$args = array(
    'post_type' => 'custom_posttyp',
    'order_by' => 'post__in',
    'post__in' => $post_ids,
);

$the_posts = get_posts( $args );

foreach( $post_ids as $id ){
    foreach( $the_posts as $a_post ){
        if( $id == $a_post->ID ){
            echo get_the_title( $a_post );
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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