如果有人在使用这个(像我一样),这里有一个更新版本。我相信这会更快,因为它不需要get_userdata()
在每个岗位上。
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;
}
}
最大的变化是:
...
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;
我使用发布日期作为
$post_dates
(Assoc阵列)。根据论点
\'ASC\'
将按升序或降序排列。
需要考虑的一件事是,生成的Assoc数组将包含WP\\u Post(在[\'post\']
) 和WP\\U用户(在[\'author\']
).