Skip to main content

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 to do. Or

Or is there aany better way to reach the goal?

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?

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 to do.

Or is there any better way to reach the goal?

Adjusted code formatting and some minor changes
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251

Multisite - sort merged queries? get_posts() queries

I am very new to this and I was trying for hours now :/

I have a multisite (test) installation of wordpressWordPress 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.

Please help meHow to sort the array :D? 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;

$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?

Multisite - sort merged queries? get_posts

I am very new to this and I was trying for hours now :/

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.

Please help me to sort the array :D 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 to reach the goal?

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?

Source Link

Multisite - sort merged queries? get_posts

I am very new to this and I was trying for hours now :/

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.

Please help me to sort the array :D 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 to reach the goal?