I think you should stick to WP_User_Query
. There you can simply specify the fields you want to use to select certain users. Also meta fields.
WP_User_Query
is a class, defined inwp-includes/user.php
, that allows querying WordPress database tableswp_users
andwp_usermeta
.
But yes(!), this will not return you user meta values. Even when setting 'fields' => 'all_with_meta'
.
all_with_meta
currently returns the same fields asall
which does not include user fields stored inwp_usermeta
. You must create a second query to get the user meta fields by ID or use the__get
PHP magic method to get the values of these fields.
That said, my conclusion would be to indeed use meta_query
in a WP_User_Query
to select users by certain meta values and use that result to retrieve additional meta data by calling for example get_user_meta($user->ID, 'meta_key', TRUE)
.
$user_query = new WP_User_Query([
'role' => 'editor',
'meta_query' => [
[
'key' => 'meta_key',
'compare' => 'EXISTS', // Use 'NOT EXISTS' for non-existance of this key.
],
],
]);
$editors = $user_query->get_results();
foreach ($editors as $editor) {
$first_name = $editor->display_name;
$meta_value = get_user_meta($editor->ID, 'meta_key', TRUE);
// ...
}
That’s one hundred times more readable and maintainable than a custom database query, in my opinion.
And regarding performance, maybe check out this related answer on the same topic just for post meta data: Can WP_Query
return post meta in a single request? which links you to: Custom post meta field effect on the performance on the post.