Put this in your functions.php
:
function get_users_ordered_by_post_date($args="") {
// Prepare arguments
if (is_string($args) && '' !== $args)
parse_str($args, $args);
$asc = (isset($args['order']) && 'ASC' === strtoupper($args['order']));
unset($args['orderby']);
unset($args['order']);
// Get ALL users
$users = get_users($args);
$post_dates = array();
if ($users) {
// For EACH user ...
foreach ($users as $user) {
$ID = $user->ID;
// ... get ALL posts (per default sorted by post_date), ...
$posts = get_posts('author=".$ID);
$post_dates[$ID] = "';
// ... then use only the first (= newest) post
if ($posts) $post_dates[$ID] = $posts[0]->post_date;
}
}
// Sort dates (according to order), ...
if (! $asc)
arsort($post_dates);
// ... then set up user array
$users = array();
foreach ($post_dates as $key => $value) {
// $user = get_userdata($key);
// $users[] = $user->ID;
$users[] = get_userdata($key);
}
return $users;
}
The above function returns an array of WP_User objects.
// EDIT: the function now supports an args parameter (either string or array). The output is ordered with respect to order=DESC
and order=ASC
. The order_by
parameter is remove (we want to order by last post date anyway), all other parameters are passed to get_users
.
Now, you can use the function like so:
get_users_ordered_by_post_date();
or
get_users_ordered_by_post_date('role=author');
or
// order by OLDEST post first
get_users_ordered_by_post_date('role=administrator&order=ASC');