I am building out a website that has about 7500 users. What i wanted was to grab all of my users and their meta data and return it to me through a rest api call that I built myself. Currently I am having a hard time returning all of our users meta data and not just one single user's meta data.
I have already built out the custom endpoint and can successfully return users
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'map/v1', 'test', array(
'methods' => 'GET',
'callback' => 'custom_phrase',
)
);
}
function custom_phrase() {
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach($users as $user_id){
return (get_user_meta ( $user_id->ID));
}
}
I expect that the endpoint will return all of my users meta_data & user_data together instead of just the final user in the index.
UPDATE: I feel like I am almost there but each index in the array comes back with the exact same information. Not to mention i am somehow unsuccessful at grabbing the Active_Memberships values which is an array before it gets to me.
function custom_phrase($request_data) {
$parameters = $request_data->get_params();
$state = $parameters['state'];
$users = get_users(array(
'meta_key' => 'active state membership',
'meta_value' => $state,
));
$stack = array();
foreach($users as $user_id) {
$user_meta = get_user_meta ($user_id -> ID);
$obj->display_name = $user_id -> display_name;
$obj->memberships = $user_meta -> active_memberships;
array_push($stack, $obj);
}
return $stack;
}
You use return in your loop, so it will return at the first iteration.
Just create a variable to store meta :
function custom_phrase() {
$users = get_users( array( 'fields' => array( 'ID' ) ) );
$data = [];
foreach($users as $user_id){
$data []= get_user_meta ( $user_id->ID);
}
return $data;
}