Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
    
                                            <li>
                                            <a href="<?php echo $string; ?>$
                                            </li>
    
                                             <?php
                                            endwhile;                      $
                    restore_current_blog();
    
                            }
         }
    ?>
    

    ?>

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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 ] ) )
                $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  ) );
        }
    }

    restore_current_blog();

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

        // Display!
        echo '<ul>';
            echo implode( "\n", $items );
        echo '</ul>';       
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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