Query to return maximum of one post per author

You need to GROUP BY the author ID, which is going to require a filter on posts_groupby. The Codex page for that filter is non-existent but it works like posts_join. Something like…

function filter_authors($groupby) {
  global $wpdb;
  $groupby = " {$wpdb->posts}.post_author";
 return $groupby;
}
add_filter('posts_groupby','filter_authors');

$args = array(
 'showposts' => 3,
 'author' => "1,2,3"
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();
 echo $post->post_title.' :: '.$post->post_author."<br/>";
endwhile;

Use your own values in $args of course.

that will effect any other query on the page that runs after this block of code. You might want to remove the filter after you are done.

remove_filter('posts_groupby','filter_authors');

Leave a Comment