Order get_users() by last login date. Is it possible?

First, 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); 

Leave a Comment