How to Group Articles or Post by Author

Grouping articles in specific criteria, like category, date or alphabet, could always help visitors to quickly find one or two posts. In my previous tutorial, I’ve shared a code snippet to group posts aphabetically. This time, I’ll share the code to group posts by their author.

How to Group Articles or Post by Author

This code will list post authors, their ID, their posts and their gravatar, like this :

Author ID1, Reza Erauansyah :
[Gravatar]
Post 1
Post 2
etc etc

Author ID2, Boss Reza :
[Gravatar]
Post 1
Post 2
etc etc

etc etc

How To

And here’s the code :

<?php
$blogusers = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE 1=1 ORDER BY display_name" );
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->ID);
    $args=array(
      'author' => $user->ID,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
      echo get_avatar( $user->ID, 46 );
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
  wp_reset_query();
  }
}
?>

You can put this code in the sidebar or wherever you want or in a page template to create a site map.

Hope this code helps.

Credit:
Thanks to queesy and MichaelH from WordPress.org

1 Comment to “How to Group Articles or Post by Author”

Add Comments (+)

  1. Mario says:

    Hello

    Can I list only authors from a certain category using this code?

    Thanks

Trackbacks/Pingbacks

  1. [WordPress Snippet]How to Group Articles or Posts by Author | WebDevKungfu
  2. 50+ Best WordPress Tips and Tutorials of February 2011
  3. wp-popular.com » Blog Archive » How to Group Articles or Post by Author

Leave a Reply

 

Amazingly Beautiful WordPress Themes