Back WordPress

How to Group Articles or Posts by Author

WRITTEN BY ON 21 Feb 2011
9,451 VIEWS • SHARES
1 comment

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. Read More: http://www.dynamicwp.net/articles-and-tutorials/how-to-group-articles-or-posts-by-author/

PHP

<?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();
  }
}
?>

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

1 comment
Odirlon 8 years ago
How to order by date from posts?
Reply