functions.php
function sorted_author_posts( $authors, $posts_num ) {
$all_posts = array();
$sort_date = array();
$posts_num = (int)$posts_num;
foreach ( (array)$authors as $author ) {
$author = (int)$author;
$author_data = get_userdata( $author );
$author_posts = get_posts( "posts_per_page=$posts_num&author=$author" );
if ( $author_posts ) {
$all_posts[$author_data->display_name] = $author_posts;
$sort_date[$author_data->display_name] = $author_posts[0]->post_date;
}
}
array_multisort( $sort_date, $all_posts );
$all_posts = array_reverse( $all_posts );
return $all_posts;
}
テンプレートの記述例
<?php $users = array( 1, 2 ); $users_posts = sorted_author_posts( $users, 2 ); foreach ( $users_posts as $user_name => $user_posts ) : ?> <h2><?php echo $user_name ?></h2> <ul> <?php foreach ( $user_posts as $post ) : setup_postdata( $post ); ?> <li><?php the_title(); ?></li> <?php endforeach; ?> </ul> <?php endforeach; wp_reset_postdata(); ?>