WordPress 如何根据上次发表文章的时间对作者用户列表进行排序
wordpress的get_users是获取用户列表,我们可以获取到所有角色为author的用户,但是这种排序并没有按照最新发表文章时间对用户列表进行排序(get_users order by author last post date)。
我们可以使用以下函数来做到这一点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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; } |
只需将 get_users 替换为 get_users_ordered_by_post_date,参数仍然作为 $args 传入。
需要注意的一点是,如果想分页获取作者列表,需要对数组进行分页。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$authors = get_users_ordered_by_post_date( array( 'role__in' => array( 'author' ) ) ); if($authors){ $perpage = 16; $pagess = ceil(count($authors) / $perpage); if (!get_query_var('paged')) { $paged = 1; }else{ $paged = $wpdb->escape(get_query_var('paged')); } $offset = $perpage*($paged-1); $authors2 = array_slice($authors,($paged-1)*$perpage,$perpage); if($authors2){ foreach($authors2 as $author){ } } } |
排序有个问题就是当用户和文章越来越多的时候,数据查询的时间会越来越长,也就是可能会卡住。
由于网站搬家,部分链接失效,如无法下载,请联系站长!谢谢支持!
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress 如何根据上次发表文章的时间对作者用户列表进行排序
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress 如何根据上次发表文章的时间对作者用户列表进行排序