I am trying to fetch users
All information by using below code.
if($_POST['orga_id']) {
$users_query = new WP_User_Query(array(
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $_POST['search'],
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $_POST['search'],
'compare' => 'LIKE',
),
),
array(
'key' => 'organisation',
'value' => $_POST['orga_id'],
'compare' => 'LIKE',
),
array(
'key' => 'available',
'value' => 1,
'compare' => '=',
),
)
));
} else {
$users_query = new WP_User_Query(array(
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $_POST['search'],
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $_POST['search'],
'compare' => 'LIKE',
),
),
array(
'key' => 'available',
'value' => 1,
'compare' => '=',
),
)
));
}
//$users_found = $users->get_results();
$args = array(
'fields' => 'all',
'orderby' => array( 'last_name' => 'asc', 'first_name' => 'asc' ),
'meta_query' => $users_query,
);
$users2 = get_users($args);
But I am getting only few information like below.
ID: "548"
display_name: "AaronKew"
user_activation_key: ""
user_email: "[email protected]"
user_login: "AaronKew"
user_nicename: "aaronkew"
user_pass: "$P$BauZua136ZxKPY9Nu2FpmZT1LRmOLr1"
user_registered: "2022-02-06 03:17:50"
user_status: "0"
user_url: ""
I am not getting first_name
and last_name
of users.
I am trying to fetch users
All information by using below code.
if($_POST['orga_id']) {
$users_query = new WP_User_Query(array(
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $_POST['search'],
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $_POST['search'],
'compare' => 'LIKE',
),
),
array(
'key' => 'organisation',
'value' => $_POST['orga_id'],
'compare' => 'LIKE',
),
array(
'key' => 'available',
'value' => 1,
'compare' => '=',
),
)
));
} else {
$users_query = new WP_User_Query(array(
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $_POST['search'],
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $_POST['search'],
'compare' => 'LIKE',
),
),
array(
'key' => 'available',
'value' => 1,
'compare' => '=',
),
)
));
}
//$users_found = $users->get_results();
$args = array(
'fields' => 'all',
'orderby' => array( 'last_name' => 'asc', 'first_name' => 'asc' ),
'meta_query' => $users_query,
);
$users2 = get_users($args);
But I am getting only few information like below.
ID: "548"
display_name: "AaronKew"
user_activation_key: ""
user_email: "[email protected]"
user_login: "AaronKew"
user_nicename: "aaronkew"
user_pass: "$P$BauZua136ZxKPY9Nu2FpmZT1LRmOLr1"
user_registered: "2022-02-06 03:17:50"
user_status: "0"
user_url: ""
I am not getting first_name
and last_name
of users.
2 Answers
Reset to default 2Actually since you already have the ID you could just loop through and get the meta.
https://developer.wordpress.org/reference/functions/get_user_meta/
get_user_meta(ID)
There's been a fundamental misunderstanding of how meta_query
works. meta_query
is meant to be an array, not a new WP_User_Query
.
This is what you've done:
get_users( [
...args
'meta_query' => new WP_User_Query( [ ...meta args ] ) // incorrect usage!
] );
This is what you should do instead:
$query = new WP_User_Query( [
... args,
'meta_query' => [ .... meta args ]
] );
Remember, get_users
is a wrapper around WP_User_Query
, and WP_User_Query
is for finding users, not specifying meta parameters.
WP_User_Query
, what resources told you to do it this way? – Tom J Nowell ♦ Commented Feb 17, 2022 at 13:41meta_query
. – Foysal Commented Feb 17, 2022 at 14:07