Skip to main content
2 of 3
Adjusted code formatting and some minor changes
birgire
  • 67.8k
  • 7
  • 119
  • 251

Multisite - sort merged get_posts() queries

I have a multisite (test) installation of WordPress and want to get the posts of every blog.

After hours, I got the solution for that. But now, I have the merged query and like to sort the posts from each blog by date. Now I get the blog1-posts and after all blog1-posts, I get the blog2-posts.

How to sort the array? Here is the code so far:

$custom_posts = array();
$blog_ids = wp_get_sites();
foreach ($blog_ids as $key=>$current_blog) {
    switch_to_blog($current_blog['blog_id']);
    $query = get_posts("posts_per_page=10");
    $custom_posts = array_merge($custom_posts, $query);
    restore_current_blog();
}

//SORT THE $custom_posts BY POST_DATE? HOW? :(

global $post; 
foreach($custom_posts as $post) : setup_postdata($post);
<<< theposts >>>.
.
endforeach;

I hope this is possible. Or is there a better way to reach the goal?