Query to Get All Users from wp_users With NO matching user_id In wp_usermeta

I'm currently trying to get all users from wp_users that have no matching ID entry in wp_usermeta (matching on user_id).

I have a site with a very large user base that currently has thousands of spam users.

The following query is returning no records:

SELECT * FROM wp_users
WHERE ID NOT IN (
SELECT user_id FROM wp_usermeta);

I've already removed the bad records from the meta table which is why I need to query to find only the records with no associated ID in wp_users.

Topic query Wordpress

Category Web


$user_query = new WP_User_Query( array ( 'orderby' => 'ID', 'order' => 'DESC' ) );
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>'. $user->ID . '</p>';
    }
} else {
    echo 'No users found.';
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.