Jesus I fought this for so long I had to write it up.
So I wanted to throw a bunch of id’s at the WordPress function get_posts() and have them come back as a nice array that I can do stuff with.
The code for this was pretty simple:
$args = array( 'post_type' => '<my_custom_post_type>', 'posts_per_page' => -1, 'post__in' => <my_array> ); return get_posts($args);
This works nice in that it gets every post that corresponds to <my_custom_post_type> and has an id in <my_array>.
But it brings them back in a willy nilly order. Uselss! I’d ordered the id’s in that array for a reason.
Turns out all I needed to do was add a further parameter ‘orderby’ to the arguments:
$args = array( 'post_type' => '<my_customer_post_type>', 'posts_per_page' => -1, 'post__in' => <my_array>, 'orderby' => 'post__in' ); return get_posts($args);
The resulting array now honours the order of the post id’s in <my_array>. Perfect!
Why do these things take so long to find out and give me such a headache.