admin管理员组文章数量:1317742
I want to create a page that displays all the blog users ordered by the last login date.
I've tried with the get_users() function and I can succesfully get the users' list, but not in the order I want:
$query = get_users('&offset='.$offset.'&orderby=login&order=DESC&number='.$number);
I think that the orderby=login is not what I'm looking for... Is there any other way to accomplish this?
I want to create a page that displays all the blog users ordered by the last login date.
I've tried with the get_users() function and I can succesfully get the users' list, but not in the order I want:
$query = get_users('&offset='.$offset.'&orderby=login&order=DESC&number='.$number);
I think that the orderby=login is not what I'm looking for... Is there any other way to accomplish this?
Share Improve this question asked Jan 20, 2014 at 19:10 stefano1stefano1 1671 gold badge6 silver badges17 bronze badges 02 Answers
Reset to default 9First, you need to store the actual login date, because this is not stored by default. You can use this code to do that(use it in your functions.php)
add_action('wp_login','user_last_login', 0, 2);
function user_last_login($login, $user) {
$user = get_user_by('login',$login);
$now = time();
update_usermeta( $user->ID, 'user_last_login', $now );
}
After that, you can use the meta field to sort the results:
$query = get_users('&offset='.$offset.'&orderby=meta_value&meta_key=user_last_login&order=DESC&number='.$number);
passatgt's answer can be simplified slightly. No need to grab the user object, when it's already present as the second argument:
add_action( 'wp_login','prefix_save_user_last_login', 0, 2 );
function prefix_save_user_last_login( $login, $user ) {
update_usermeta( $user->ID, 'user_last_login', time() );
}
本文标签: functionsOrder getusers() by last login date Is it possible
版权声明:本文标题:functions - Order get_users() by last login date. Is it possible? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742019066a2414323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论