Skip to content

Get user by metadata

Getting user by field name is easy, #WordPress have function for that get_user_by. Getting user by his metadata is a little bit complicated. You can found on the Internet some complex procedures how to get user by metadata using WP_Query object. Forget about them! You need prepare correct meta query as array or string and call get_users. Thats all!

function get_user_by_meta($meta_key, $meta_value) {
return reset(
get_users(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'number' => 1,
'count_total' => false
)
)
);
}