To obtain multiple posts in accordance with the criteria that we give becomes easier with WP Query class. WP Query can be used to get the latest posts, based on post meta value, number of comments, post types, and so on.There is also a function to get posts by using get_posts() that have same parameters of WP_Query because it uses a new instance of it.
Let’s assume you already have some post ids as an
array(3,5,1,4). To get the posts by the given post ids:
$posts = get_posts( array( 'post__in' => array(3,5,1,4), 'orderby' => 'post__in', 'order' => 'ASC' // won't work ));
Please note, the
order by ascending or descending parameter will not work for this argument. If you want to sort it in reverse order, the code below will do it.
$posts = get_posts( array( 'post__in' => array_reverse( array(3,5,1,4) ), 'orderby' => 'post__in' ));
$ids = array(3,5,1,4); rsort( $ids ) // or reverse: sort( $ids ); $posts = get_posts( array( 'post__in' => $ids, 'orderby' => 'post__in' ));
That’s how to sort by the posts ids, if you have any ideas for sorting this ids, please feel free to add_action for the comment box below.