0

I am using count_users() function to count users on my wordpress sites. In my administration area -> 'Users' I can see 7 users, but the function returns 9. After some time, searching for errors in my code, I noticed that in the admin area there are 7 users listed, but near the 'All' link there is a number counting users and the number is effectively 9.

So, what is happening? Maybe there is a trash for users, and Wordpress is still counting deleted user or something like that?

EDIT: Just for the sake of completeness, tried to delete an user and the total counter is correctly lowered by one unity. Still having two phantom users...

  • 1
    Do you have PHPMyAdmin or sth. like that? How many entries are there in your _users table? – flomei Feb 19 '16 at 10:53
  • 7, so the db is ok... – Sasha Grievus Feb 19 '16 at 10:58
1

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
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.