Skip to content

Instantly share code, notes, and snippets.

@amanhstu
Created August 1, 2023 11:55
Show Gist options
  • Save amanhstu/8a79a7ad3447459993c0512ad49a2c30 to your computer and use it in GitHub Desktop.
Save amanhstu/8a79a7ad3447459993c0512ad49a2c30 to your computer and use it in GitHub Desktop.
<?php /*Load WP functions and DB access*/
include('wp-load.php');
/*required for wp_user_delete*/
require_once(ABSPATH . 'wp-admin/includes/user.php');
/*Let it run forever*/
set_time_limit(0);
/*Get the $wpdb database object*/
global $wpdb;
$i = 0; /*counter starts at 0 not 1*/
/*log start time*/
$start_time = microtime(true);
/*Loop through all users*/
foreach ($wpdb->get_results('SELECT ID from ' . $wpdb->prefix . 'users ORDER BY ID DESC') as $user) {
/*Get user object*/
$user = get_user_by('ID', $user->ID);
/*Check if this user's role (customer, subscriber, author, etc.)*/
$roles = $user->roles;
if ($roles[0] == "customer") {
/*Check the order count and delete if it is 0*/
$order_count = wc_get_customer_order_count($user->ID);
if ($order_count === 0) {
wp_delete_user($user->ID);
/*stops at 1000th record - change this if you want*/
if ($i++ == 999) {
/*log the time it took*/
$end_time = microtime(true);
break;
}
}
}
}
if (!$end_time) {
$end_time = microtime(true);
}
$execution_time = ($end_time - $start_time);
echo "DONE! Deleted " . $i . " users.<br>";
echo "The time in " . date_default_timezone_get() . " is " . date("H:i:s");
echo " Execution time of script = " . $execution_time . " sec";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment