One of my favorite things about the WordPress API is the ability to store custom meta data with objects such as pages and users. I’ve been working on an application where this scenario has presented itself:
- The end user is able to create a company
- Each company can have an administrator based on users in the system
To do this, I’m creating a relationship between the company and user by associating the company’s ID as part of the user’s meta data.
Later in the application, I need to retrieve each administrator for each company which has required the use of a helper function to get a user by meta data.
The details of how I’m doing this are aren’t exactly interesting for this post, but I figured that this was a common enough issue and easy enough to generalize that I’d share how to programmatically get a user by meta data in WordPress: