任何一个在这几年后来到这里的人,都希望得到完整的代码,而不是从评论和公认的答案中拼凑出来,来吧!
Declare the function that limits the post results to 1 per author. 对我来说,这是在函数中。php:
function one_per_author($groupby) {
global $wpdb;
$groupby = "{$wpdb->posts}.post_author";
return $groupby;
}
请勿使用以下内容
apply_filter
根据公认的答案,这将适用于所有循环。
Next run the loop you would like to filter, 打电话给apply
和remove
直接在其前后过滤:
$args = array (
\'post_type\' => array( \'product\' ),
\'posts_per_page\' => \'8\',
\'order\' => \'DESC\',
\'orderby\' => \'date\',
);
add_filter( \'posts_groupby\', \'one_per_author\' );
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// Do loop stuff
endwhile;
endif;
remove_filter( \'posts_groupby\', \'one_per_author\' );