I am trying to remove inactive Woocommerce users (that have never made an order AND have been inactive for the past 6 months) using custom queries. below query, works find in PHPMyAdmin but in WordPress its not working.
global $wpdb;
$user_table_name = $wpdb->prefix . 'users';
$postmeta_table_name = $wpdb->prefix . 'postmeta';
$post_table_name = $wpdb->prefix . 'posts';
try {
$wpdb->query($wpdb->prepare("DELETE from $user_table_name as u where u.ID not in (
SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_customer_user'
) AND u.ID not in (
select distinct(post_author) from $post_table_name
) AND u.user_registered <= (now() - interval 6 month) limit 2"));
//return true;
} catch (Exception $e) {
return 'Error! ' . $wpdb->last_error;
}
I appreciate it if someone can help me with this.