Hi I’m trying to make a query to get posts from a specific categories like this:
$args = array('category__in' => array(8,3,12,7));
$posts = new WP_Query($args);
But I need the posts to be displayed in that specific order ( cat id 8 first, then 3, etc ), I can’t get it to work properly, posts are displayed according to ASC or DESC names.
Any Help?
You can sort by
post__in
to use the order of the input values, but withcategory__in
it is a many to many relationship, so using it order by is considerably more difficult and is not supported as far as I know. Also note thatWP_Query()
does not return an array of posts.If you have your specific set of ordering rules you can take the results from
get_posts()
using thecategory
argument, and then use a custom sorting function to order the results usingusort()
andget_categories()
.To my knowledge the best approach here is to make 4 separate queries.