I've successfully added a custom meta key / value for a user and managed to display it in the rest API however I'm not sure how I can retrieve the user based on this key.
For example
$fields = ['trdr'];
foreach($fields as $field){
register_rest_field('user', $field, [
'get_callback' => function($user, $field_name, $request) use ($field){
return get_user_meta($user['id'], $field_name, true);
},
'update_callback' => function($user, $meta_value) use ($field){
update_user_meta($user['id'], $field, $meta_value);
},
'schema' => [
'type' => 'string',
'description' => 'the customer trdr in softone',
'context' => ['view', 'edit']
]
]);
}
The field is successfully retrieved in the users when retrieving by ID but I'm not sure how I can query for the user using this specific rest field.
I'm using the composer package https://github.com/varsitynewsnetwork/wordpress-rest-api-client to simplify the process of talking to the API and for example I can do
return $this->wpClient->getClient()->users()->get(null, ['slug' => $slug]);
To retrieve a single user by his slug. However doing it for the email or the field (trdr in my case) doesn't work. Any ideas what extra steps are needed?
The library's code is really straightforward to create the request so it doesn't seem to be because of the library.
public function get($id = null, array $params = null)
{
$uri = $this->getEndpoint();
$uri .= (is_null($id)?'': '/' . $id);
$uri .= (is_null($params)?'': '?' . http_build_query($params));
$request = new Request('GET', $uri);
$response = $this->client->send($request);
if ($response->hasHeader('Content-Type')
&& substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') {
return json_decode($response->getBody()->getContents(), true);
}
throw new RuntimeException('Unexpected response');
}
WP_Query
?