function delete_inactive_users() {
$args = array(
'role' => 'customer',
'number' => -1,
'meta_query' => array(
array(
'key' => '_last_order_date',
'value' => '',
'compare' => 'NOT EXISTS'
)
)
);
$user_query = new WP_User_Query($args);
if (!empty($user_query->results)) {
foreach ($user_query->results as $user) {
wp_delete_user($user->ID);
}
}
}
add_action('admin_init', 'delete_inactive_users');
wp user list --role=customer --meta_key=_last_order_date --meta_compare=NOT EXISTS | awk '{print $1}' | xargs wp user delete --reassign=1
DELETE u, um
FROM wp_users u
LEFT JOIN wp_usermeta um ON u.ID = um.user_id
LEFT JOIN wp_wc_order_stats os ON u.ID = os.customer_id
WHERE os.customer_id IS NULL;
DELETE FROM wp_users
WHERE ID NOT IN (
SELECT DISTINCT user_id
FROM wp_usermeta
WHERE meta_key = '_last_order' AND meta_value IS NOT NULL
)
AND ID NOT IN (
SELECT DISTINCT user_id
FROM wp_usermeta
WHERE meta_key = 'wp_capabilities'
AND (
meta_value LIKE '%administrator%'
OR meta_value LIKE '%editor%'
OR meta_value LIKE '%author%'
OR meta_value LIKE '%publisher%'
)
);