Practical WordPress Development

How To Find a User By Meta Data in WordPress

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:

function get_user_by_meta_data( $meta_key, $meta_value ) {

	// Query for users based on the meta data
	$user_query = new WP_User_Query(
			'meta_key'	  =>	$meta_key,
			'meta_value'	=>	$meta_value

	// Get the results from the query, returning the first user
	$users = $user_query->get_results();

	return $users[0];

} // end get_user_by_meta_data

It’s simple, right?

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:

  • $first_name = get_user_meta( get_user_by_meta_data( 'my_meta_key', 'my_meta_value' )->ID, 'first_name', true );
  • $last_name = get_user_meta( get_user_by_meta_data( 'my_meta_key', 'my_meta_value' )->ID, 'last_name', true );

But wait, what about all users?

But there’s a catch here: this will only return the first user. Depending on the requirements of your project, you may have multiple users with the name meta value.

In that case, it may be of more interest to return the collection of users than a single user. To do that, you can generalize the above function to the following:

function get_users_by_meta_data( $meta_key, $meta_value ) {

	// Query for users based on the meta data
	$user_query = new WP_User_Query(
			'meta_key'	  =>	$meta_key,
			'meta_value'	=>	$meta_value

	// Get the results from the query, returning the first user
	$users = $user_query->get_results();

	return $users;

} // end get_users_by_meta_data

From here, you can then iterate through the list of users to find what you need:

foreach( get_users_by_meta_data( 'my_key', 'my_value') as $user ) {
	// Do something with each $user
} // end foreach

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!


  1. Curtis McHale

    Why not use WP_User_Query?

    • Tom McFarlin

      Didn’t even know it existed – thanks for sharing! Much cleaner, for sure.

      Gave you props in the post message above and updated the code (and my app) accordingly :).

      • Curtis McHale

        It was only introduced in 3.1 and hasn’t been talked about much really. I was just wondering really.

        Thanks for the props.

        • Tom McFarlin

          Of course! I may be sure to give this some coverage later this week. Will be sure to link back to this thread, as well.

  2. George

    Just want to say thanks! i have been searching high and low all day for this exact function :-) you are my hero!

  3. Vayu

    Hi Tom.
    Thanks for sharing. I need to create a similar scenario. How did you store your company type? Did you make it custom post type or did you also make it a user?

  4. Kevin

    Thank you for this, Will certainly make great use of it.

  5. Tidjy Kimisu

    You don’t need use that to get its users informations :

    $first_name = get_user_meta( get_user_by_meta_data( ‘my_meta_key’, ‘my_meta_value’ )->ID, ‘first_name’, true );
    $last_name = get_user_meta( get_user_by_meta_data( ‘my_meta_key’, ‘my_meta_value’ )->ID, ‘last_name’, true );

    Just replace the above lines by :
    get_user_by_meta_data( ‘my_meta_key’, ‘my_meta_value’ )->last_name;
    get_user_by_meta_data( ‘my_meta_key’, ‘my_meta_value’ )->first_name;

    • Tom

      This is another great options — thanks for adding this!

  6. Naresh Devineni

    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(
    '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',

    • Tom

      Hey Naresh – unfortunately, this is something that’s longer than I have time to respond to in a blog comment.

      All the best!

      • Naresh Devineni

        No problem …Thank you :)

  7. Ryan Hellyer

    This is an awesomely handy bit of code. Thanks for sharing.

    • Tom

      Thanks Ryan – glad it was helpful :).

Leave a Reply

© 2020 Tom McFarlin

Theme by Anders NorenUp ↑