这有点困难,因为没有默认的用户角色元键或post数据。我们唯一得到的是用户ID。
// Get the posts
$posts_by_author_ID = get_posts( array(
\'post_type\' => \'post\'
,\'post_status\' => \'publish\'
,\'orderby\' => \'post_author\'
,\'order\' => \'DESC\'
) );
foreach ( $posts_by_author_ID as $post )
{
static $user;
$new_user = get_user_by( \'id\', $post->post_author );
// Let\'s save some queries
if (
isset ( $user )
AND $user->ID === $new_user->ID
)
continue;
$user = $new_user;
foreach ( $user->roles as $role )
{
$posts_by_author_role[ $role ] = array(
\'user\' => $user
,\'post\' => $post
);
}
}
// Sort by key a.k.a. role
ksort( $posts_by_author_ID );
echo \'<pre>\'; // Only for debugging/developing - should get deleted when done
foreach ( $posts_by_author_ID as $role => $data )
{
list( $user, $post ) = $data;
// Now we can output our data
// The user data is saved inside $user
// The actual post data is saved inside $data
// The following is only for debugging/developing - should get deleted when done
var_export( $user, false );
var_export( $post, false );
}
echo \'</pre>\'; // Only for debugging/developing - should get deleted when done