WordPress has a trash for users? (Wrong result for count_user, greater than expected)

No, the user system does not have a trash (this is the default, obviously you can add pretty anything).

My problem was caused by users meta… If you take a look at the the function count_users (located in wp-includes/user.php), you’ll see some comments there. It says that the function:

  • Assumes there are neither duplicated nor orphaned capabilities meta_values.
  • Assumes role names are unique phrases.

In my case, there were orphan meta values. Deleting from wp_usermeta values having a user_id not present in wp_users table solved the problem.

Delete can be performed with a mysql query like this

DELETE FROM wp_usermeta
WHERE NOT EXISTS (
  SELECT * FROM wp_users
    WHERE wp_usermeta.user_id = wp_users.ID
)

Leave a Comment