Is there some kind of MySQL query cache turned on by default or something? I am trying to work on a user import script and I am seeing some strange results from the username_exists
function after I delete records from the wp_users
table. After I delete the records when I call username_exists
WordPress is still returning the ID for the deleted user instead of false
. Odd. What am I missing?
2 Answers
By default, WordPress uses $wp_object_cache
which is an instance of WP_Object_Cache
to save on trips to the database.
Trace back username_exists()
function, you will notice that get_user_by()
uses WP_User::get_data_by()
which return user data from $wp_object_cache
immediately without checking if that user exists in database. Note that $wp_object_cache
is stored in memory.
It means that if you deleted an user record directly in your database without cleaning up user cache, user data is still available. That’s the problem.
So what you’re missing?
-
Missing to delete user the right way.
-
Missing to clean user cache.