Created
August 3, 2023 16:11
-
-
Save amanhstu/577e4cd2b68ab9eea093b58b3ac7ebd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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