The Magic of the get_users() Function

While working on Email Users recently I encountered a performance problem with some SQL which I had received via the WordPress Hackers Mailing List back in January of 2008.  At the time I needed it for my wp-SwimTeam plugin (where I am still using it) and it served me well.  When I first committed a patch for Email Users (v3.4.0) I used this SQL to allow sorting of the users as prior to v3.4.0, only the usernames were displayed.

It turns out this SQL is pretty inefficient (I am by no means an SQL expert) and it was causing performance problems for people using Email Users with a large number of users.  Again I turned to the WP Hackers Mailing List and received some significantly improved SQL.

The ensuing discussion also recommended migrating my SQL to the WordPress API get_users() function.  I am all for using the API when I can as it is much safer and typically much easier to work with.  In this case, I couldn’t understand how it would help me retrieve the first and last names from the Meta table that I needed.  It wasn’t mentioned anywhere in the Codex so I was confused.

A subsequent post in the thread mentioned the “magic” of get_users() which I then tried and sure enough, the data I wanted was available.  Ok, now I am really confused.  Where does this magic data come from.  Fortunately another post referenced this article which explains how these magic methods work and why the data is available.  This is pretty neat and very useful although I have no idea how developers would know this exists based on the documentation in the Codex.

These “magic methods” strike me as one of those “inside baseball” things that not being a full time WordPress developer I’d never know about.  It is very useful and something I’ll certainly remember and use in the future.  Thanks to the WordPress Hackers Mailing List – an incredibly valuable resource.