WP Query post__in not returning correct results

(Revised to improve the wording)

Actually, you do not have to set the orderby to post__in for post__in query to work. There’s no such limitation in WordPress and you’d only need to set 'orderby' => 'post__in' when you want to sort the posts exactly in the same order as the post IDs in the post__in parameter. 🙂

And the actual problem in your code, is that WordPress (or the WP_Query class) doesn’t recognize your post__in parameter because there is a trailing space inside the parameter name — PHP doesn’t automatically remove trailing spaces in array keys or even values, and so does WordPress (i.e. the spaces are preserved); so $args['post__in '] (note the space) and $args['post__in'] are two different array items:

$args = array( 'post_type' => 'projects', 'post__in ' => $projectIDs, 'posts_per_page' => 3 );

And even in the var_dump() output, you could quite clearly see the trailing space here:

["post__in "]=>

So the fix is simple: remove the space. And try again with your query, where you should see it now has the proper IN clause:

AND wpKX_posts.ID IN (79,98,108)

And actually, you don’t need to manually add the indexes here:

//$projectIDs = array( [0] => 79, [1] => 98, [2] => 108 );
$projectIDs = array( 79, 98, 108 ); // no manual indexes; still would work fine

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)