I use the following code for getting the authors of in my multi-author blog and order them by their post count. I want to order them by the date of their last post
$authors = get_users('role=author&orderby=post_count&order=DESC');
I use the following code for getting the authors of in my multi-author blog and order them by their post count. I want to order them by the date of their last post
$authors = get_users('role=author&orderby=post_count&order=DESC');
Share
Improve this question
asked Apr 18, 2013 at 10:16
MidhunMidhun
331 silver badge4 bronze badges
2
|
2 Answers
Reset to default 5Put 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');
In case someone is using this (like me), here's an updated version. I believe this is quicker since it doesn't require get_userdata()
on each post.
function get_users_ordered_by_post_date_dev($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']);
//Date = today
$today = date( 'Y-m-d' );
// Get ALL users
date_default_timezone_set('America/New_York');
$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(
array(
'author' => $ID,
'date_query' => array(
'year' => (int)date('Y'),
'month' => (int)date('n'),
'day' => (int)date('j'),
'compare' => '=',
'column' => 'post_date',
)
)
);
if ($posts) $post_dates[$posts[0]->post_date] = [
'author' => $user,
'post' => $posts[0]
];
}
if (! $asc){
krsort($post_dates);
}else{
ksort($post_dates);
}
return $post_dates;
}
}
The big change is here:
...
if ($posts) $post_dates[$posts[0]->post_date] = [
'author' => $user,
'post' => $posts[0]
];
}
if (! $asc){
krsort($post_dates);
}else{
ksort($post_dates);
}
return $post_dates;
I'm using the post dates as the key in $post_dates
(Assoc Array). And depending on the argument 'ASC'
will be ordered ascending or descending.
One thing to consider is that the resulting Assoc Array will contain both the WP_Post (under ['post']
) and WP_User (under ['author']
).
get_users
– Tom J Nowell ♦ Commented Apr 18, 2013 at 10:27