Create custom route for API, get only one user
-
Hi everybody,
I’m trying to get custom user metadata (that i set when someone subscribe) by API request.
For this purpose, I do this thing to get my users metadata en organise it:function getUsersMeta() { $myObj = new stdClass(); $users = get_users( array( 'fields' => array( 'ID' ) ) ); foreach($users as $user){ $myObj->rue = get_user_meta($user->ID,'rue',true); $myObj->cp = get_user_meta($user->ID,'cp',true); $myObj->ville = get_user_meta($user->ID,'ville',true); $myObj->structure = get_user_meta($user->ID,'structure',true); } return json_encode($myObj); }
And then where I kind of “open the route” :
add_action( 'rest_api_init', function () { register_rest_route( 'authors', '/meta', array( 'methods' => 'GET', 'callback' => 'getUsersMeta', ) ); });
But for an unknown (to me) reason, what I get is only one user :
</img>
And I’d realy like to get all them.
Also, I’ve got a noob question : is there any way to display final json in anoter way that : \”rue\”:\”20 rue Bechevelin\”, andso… so that it could be nicer when I reveice it in my .js (i think about the {first{“name”: “jack”, “age”: 22}}
Thanks a lot !!!, any help will be useful, I already tried lots of get_users() combinations and I can’t get where I’m wrong.
??
- The topic ‘Create custom route for API, get only one user’ is closed to new replies.