0

What MySQL query can I use to get a list of users who have never logged in to WP?

Some background...

My WordPress website has been running since 2015 and initially, I had a newsletter form that would also sign the user up to our website (sending them autogenerated WP credentials by email).

The problem is most users never cared about that WordPress access. They only cared about the newsletter. So much so that they never signed in to WP.

I want to then remove these users, one by one, through the WordPress Dashboard.

4
  • 2
    I don't think WP stores this data. Do they share the same role? Then you could just remove all users of that role (e.g. subscriber)
    – kero
    Nov 24, 2022 at 10:46
  • 2
    Why do you need to do this? What problem are you trying to solve that requires this?
    – Tom J Nowell
    Nov 24, 2022 at 11:35
  • @TomJNowell because I'm adding a forum (XenForo) and a plugin that will sync users between both distinct apps/db. Wanted to cleanup the deadwood before syncing. I have 50k rows so the sync would take a looooong time for no real reasons as most never logged in and barely know they have an account anyway.
    – Lazhar
    Nov 24, 2022 at 14:07
  • note that there are sites with millions of users, the only real way you can reduce this is by deleting all accounts in 2 or 3 months time that don't login after you send out an expiration notice email. You'd need to write code that logs when they last logged in though so you knew who to delete and who to keep. If it's a one time operation though then it makes more sense to just pay the one time cost. Either way the information you need to achieve what you asked does not exist, it cannot be done retro-actively.
    – Tom J Nowell
    Nov 24, 2022 at 15:03

1 Answer 1

2

What MySQL query can I use to get a list of users who have never logged in to WP?

None. WordPress does not store this information so this is not something you can do, you would need to add code for it but that would also require going back to 2015 to add the code for it to solve your problem.

WordPress doesn't hold records of previous/first/last logins, only current active sessions. These active session records are destroyed when a user logs out, and when the login expires. They're also approximate, not accurate. This also means you can't use them to figure out when a user was last active, when they last logged in, or if they have ever logged in.

At best, you could add code to log when a user logs in to user meta, then wait a few months and see who has and has not logged in. Then you would have the information needed to do this, and you would do it via WP_User_Query and wp_delete_user.

Right now though, you don't have the information to do this. Note that doing it via SQL would mean stale caches and other broken-ness, you should do it via WordPress APIs, be that the REST API, CLI, or in PHP.

1
  • @Lazhar if this answers your question consider marking it as the correct answer. If you are holding out for an answer that says yes it can be done then you may be waiting a very long time. The information to identify which users to delete and which to keep doesn't exist.
    – Tom J Nowell
    Nov 24, 2022 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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