To keep the same post order as it was passed to the post__in
parameter, you need to pass post__in
as value to orderby
argument and ASC
to the order
argument
$query_args = [
'posts_per_page' => $products,
'no_found_rows' => true,
'post_type' => 'product',
'post__in' => [161,165,131,202],
'orderby' => 'post__in',
'order' => 'ASC'
];
EDIT
Looking at your edit, you have a bad filter somewhere, most probably a bad pre_get_posts
action or a posts_orderby
filter. Do the two following tests
-
Add
'suppress_filters' => true,
to your query arguments, if that helps, you have a bad filter -
Add
remove_all_actions( 'pre_get_posts' );
before you do your query, if that helps, you have a badpre_get_posts
filter -
A third, but most probable useless check would be to add
wp_reset_query()
before the custom query, if that helps, you have a bad query somewhere, most probably one usingquery_posts