I want to show 8 recent products from multiple authors on the frontpage.
Need to show 1 recent product per author.
Currently i’m using basic wp query to show recent 8 products.
$args = array (
'post_type' => array( 'product' ),
'posts_per_page' => '8',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
What’s the best way to achieve this?
2 Answers
I believe that you can achieve this effect by grouping the query by author ID, which will require a filter(mostly cribbed from the Codex):
function my_posts_groupby($groupby) {
global $wpdb;
$groupby = "{$wpdb->posts}.post_author";
return $groupby;
}
add_filter( 'posts_groupby', 'my_posts_groupby' );
If you have less than 8 authors, however, this won’t work. (You will get a number of results equal to your number of authors.) If that is the case you will need much more complicated logic and a much more complicated code.