How To Find a User By Meta Data in WordPress

September 26, 2012

Thanks to Curtis for giving a heads up to WP_User_Query - it's a nicer alternative to the original post.

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:

Get User By Meta Data

In my case, the meta key value is fixed, so I’ve generalized it for the purposes of this particular post where you can pass in a specific key and value, though depending on the requirements of your application – such as in my case – the key could be hard coded and the meta value could be the variable:

Basically, you specify the key and value for the set of users that you want to retrieve and then return the first one in the array.

At this point, you have access to all of the fields associated with a user such as its ID, display_name, and so on. You can also get the rest of its information, such as the first_name and last_name by using the ID and a call to get_user_meta:

And what about get_user_meta?

In order to use get_user_meta, you have to have a user. If you’ve already got the user ID and the meta key, then you know exactly what you need. The thing is, you may not always know exactly what user – or users – have certain data stored so it helps to have a utility function for doing just that.

Perhaps this is a niche case, but I’ve used it in two applications at this point, so it’s not niche enough to share with you guys!

Hi Tom, How are you? I have been using wp_user_query quite extensively in a school website that i have developed recently. I used it for teachers to see their students details. They call it as Phone tree. It worked really well untill last few weeks. When I debugged the code, I have found a strange issue. If you observe the code below, There are four meta query arrays. Syllabus, Class , Section and Branch. Though there are students with those profiles, results are not found. But If I am removing any one of those four meta queries, lets just say Branch, It is working and student records are being found. But if when I include a fourth meta query array, no records are being returned. Form contains only select boxes with generated values from database. I can’t blame the data. What could be the issue? // Generate the query based on search field $my_users = new WP_User_Query( array( 'role' => $role, //'search' => '' . $search . '', //'search_columns' => array( 'user_login', 'user_email','user_nicename','display_name' ), 'meta_query' => array( 'relation' => 'AND', 0 => array( 'key' => 'syllabus', 'value' => $syllabus, ), 1 => array( 'key' => 'class_code', 'value' => $class_code, ), 2 => array( 'key' => 'section', 'value' => $section, ), 3 => array( 'key' => 'branch', 'value' => $branch, ), ), 'fields' => 'all_with_meta', ));

Just Getting Started with WordPress?

I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.

WPSessions hosts some of the best WordPress training you’ll find anywhere from many well-known speakers.

If there’s something you’d like to learn, and it’s not already covered here, it’s probably been covered at WPSessions. If you use the special link below you can watch your first session for free and get a steep discount on the full-access VIP membership.