I have added this to the users as they log into any of the sites on my multisite.
update_user_meta( $user_id, '_last_login', time() );
Now I want to delete all of the users that have not logged in for the past 6 months. I am looking for the sql version of this as I will then include it into a scheduled task to run once a week to clean the database.
$time = strtotime('-1 hour');
$user_query = new WP_User_Query(array('meta_key' => '_last_login', 'meta_value' => $time, 'meta_compare' => '<=', 'fields' => 'ID'));
if (!function_exists('wpmu_delete_user')) {
include(ABSPATH . "wp-admin/includes/ms.php");
}
if ($user_query->results) {
foreach ($user_query->results as $old_user) {
wpmu_delete_user($old_user);
}
};
If your code works, use it.
It ought to be possible to do this in pure SQL and that might even be more efficient, but don’t. Rather, unless you are experiencing identifiable issues using the Core code– timeouts, for example– and cannot solve them, don’t resort to a pure SQL solution.
The reason is that if the database structure or other Core functionality changes, then code based on the Core functions should (probably) survive. Code based in pure SQL might not.