How to get one user by his metadata?

How to get one user by his metadata?

Simple way how to get one user by his metadata is: $user = reset (get_users (array (‘meta_key’ => $meta_key, ‘meta_value’ => $meta_value, ‘number’ => 1, ‘count_total’ => false)));

How to get user data in WordPress meta query?

If you have additional metadata to test, add another array element to the meta_query array. Here is the codex page from WordPress detailing how to use the get_users ($arg); function. It contains examples how how to build custom functions to fetch various parts of user data.

How to search for a user by Meta key?

Since WP v3.1 it’s ridiculously easy to search for a user by his/her meta key. Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.

How are custom fields created in meta query?

The approved and available are custom fields created using ACF as True/False fields. If you have additional metadata to test, add another array element to the meta_query array.

How to update user meta for multiple meta key?

So instead of passing your array to update_user_meta which only accepts string arguments for $meta_key, loop over the array and call update_user_meta for each key/value pair in the array. WordPress doesn’t give a built in way to update multiple metas at once.

What happens if the meta value is empty?

Please note that if the meta value exists but is empty, it will return an empty string (or array) as if the meta value didn’t exist. This might cause unexpected behaviors in your code when you empty the user meta, your code can try to use add_user_meta instead of update_user_meta thinking the user does not have meta created yet.