Find most recent authors

The question:

What I’m looking to do it get the 16 latest posts from 16 different authors. In other words there would be the 16 latest posts, but no one single author would have more that 1 post on the page.

The way I’ve thought about doing it, is looping through all the authors, removing the duplicates, then limited that to 16 and use that to generate the posts in a for each loop, but I’d imagine that would be heavy on the server.

Any suggestions?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Use a custom SQL query to grab the latest 16 post IDs with unique authors;

$post_IDs = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish' GROUP BY post_author ORDER BY post_date DESC LIMIT 16" );

Then start up a new query with the post__in argument;

$recent_posts = new WP_Query( array( 'post__in' => $post_IDs, 'post_type' => 'product' ) );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment