I’m trying to see if there is a way to find an average age of user accounts which came through my wordpress site not including the age of the administrator account.
When I say age I don’t mean their actual birth age but the tenure of their account.
Is there a simple way to determine this?
WordPress records when a user was registered in the
$wpdb->users
(usuallywp_users
) table in the columnuser_registered
. So you can use that to calculate the average account age. There’s no internal function for this, so you’ll have to use$wpdb
directly.This stackoverflow answer has some good info about calculating the average of a series of dates. Translated to WordPress functions and such:
If you’re admin user’s ID is
1
, then…… would be the average account age excluding the admin.
If you, say, want to exclude all administrator level users, you’ll have to fetch users via
get_users
of that role then exclude the array of those ID’s (egNOT IN
). WP doesn’t have separate tables for roles and caps, making it difficult to query by them in SQL directly — a users capabilities are stored as a serialized array.As @chrisguitarguy stated, there is no internal function to do this. Below is my suggested solution. It should ignore any user with the ‘administrator’ role.
This gives a sane average when tested on my server. It returns an answer in “days”, by the way.
I am toying with some variants. I may post other versions.