WP_Query Order by array specific post ids

I used post__in key to get posts from the specific post ids and order order posts by the other array specific post ids.

I have following:

$post_ids=array(100,200,300,400,500,600,700,800,900,1000);
$orderby_post_ids = array(300,800,500);
//$order=Ascending/ Descending;

If it Ascending order, expected output should be like this,

300,500,800,100,200,400,600,700,900,1000

If it Descending order, expecting output should be like this,

800,500,300,100,200,400,600,700,900,1000

How do I achieve above result using WP_Query? I used below arguments, but the result is not as expected.

$args = array(
‘post_type’ => ‘mycourses’,
‘posts_per_page’ => 4,
‘orderby’ => ‘post__in’,
‘order’ => ‘ASC’,
‘post__in’ => $post_ids,
);

Thanks in advance.

Read more here:: WP_Query Order by array specific post ids

Leave a Reply

Your email address will not be published. Required fields are marked *