You can try the new (4.7+) wp_list_sort()
utility function that’s a wrapper for WP_List_Util::sort()
that uses usort()
or uasort()
.
Example 1:
Sort by descending post date
$sorted_custom_posts = wp_list_sort(
$custom_posts,
'post_date',
'DESC'
);
Example 2:
Sort by descending post date and ascending post title
$sorted_custom_posts = wp_list_sort(
$custom_posts,
[
'post_date' => 'DESC',
'post_title' => 'ASC',
]
);
Check the Make post here for more information.