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 如何根據上次發表文章的時間對作者用戶列表進行排序