I am a newbie who needs to produce a chronological list of recent posts from all sites to which a user is subscribed on a WordPress multisite network.

Using the code below, I am able to produce a list where the posts are ordered chronologically (DESC) but grouped by site:

  1. Site A Wednesday
  2. Site A Tuesday
  3. Site A Monday
  4. Site B Wednesday
  5. Site B Tuesday
  6. Site B Monday

I need to produce a list where all posts are ordered chronologically:

  1. Site A Wednesday
  2. Site B Wednesday
  3. Site A Tuesday
  4. Site B Tuesday
  5. Site A Monday
  6. Site B Monday

            $user = get_current_user_id();
            $blogs = get_blogs_of_user( $user );
                 if( $blogs ) {
                 foreach( $blogs as $blog ) {
                    switch_to_blog( $blog->userblog_id );
                            $query = new WP_Query ('posts_per_page=2');
                                    if ($query->have_posts()) :
                                            while ($query->have_posts()) : $query->the_post();
                                            <a href="<?php echo $string; ?>$
                                            endwhile;                      $


When I have attempted to apply 'order by' to the WP_Query using (new WP_Query( array( 'order by' => 'date' ), I'm producing the first list above (a list ordered chronologically, grouped by site). I have the feeling that I'm supposed to be sending the output of WP_Query to an array, sorting the array, and then printing this to the page, but as I am extremely new to all of this, I could really use some guidance at this point. Thanks.

1 Answer 1


You can build an array of all post items, and then sort them by date at the end of your querying:

$blogs = get_blogs_of_user( get_current_user_id() );

if ( $blogs ) {
    $items = array();

    foreach ( $blogs as $blog ) {
        switch_to_blog( $blog->userblog_id );
        $posts = get_posts( 'posts_per_page=2' );
        foreach ( $posts as $post ) {
            // Use the timestamp to sort all posts by later.
            $timestamp = strtotime( $post->post_date_gmt );

            // Just in case another published at *exactly* same time.
            while ( isset( $items[ $timestamp ] ) )

            // Add our HTML for this post to the stack.
            // You can't store the post object and work on it later as we'll be outside the scope of this particular blog.
            $items[ $timestamp ] = sprintf( '<li><a href="%s">%s</a></li>', get_permalink( $post->ID ), get_the_title( $post->ID  ) );


    if ( $items ) {
        // Sort by latest to oldest.
        krsort( $items );

        // Display!
        echo '<ul>';
            echo implode( "\n", $items );
        echo '</ul>';       

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.