It looks like you want to order by post__in
:
$samp = array (
'post_type' => 'product',
'posts_per_page' => 12,
'post__in' => $init_ids_arr,
'orderby' => 'post__in', // <-- Try this to fix the order
);
We might also want to skip the sticky posts with:
'ignore_sticky_posts' => true,
If we want to display all of the user-selected posts, then we could use:
$samp = array (
'post_type' => 'product',
'post__in' => $init_ids_arr,
'nopaging' => true, // <-- No paging
'ignore_sticky_posts' => true, // <-- Avoid injected sticky posts
'orderby' => 'post__in', // <-- Keep user defined order
);
where we added the nopaging
attribute to remove the SQL_CALC_FOUND_ROWS
and LIMIT
parts of the generated SQL query.
We should remember to validate the user input, before we run the query.
Like making sure the $init_ids_arr
isn’t an empty array otherwise we get the default WHERE part of the query. We also want to make sure it’s only integers and we don’t want to query huge number of posts.