I managed to solve my issue by taking a look at \WP_REST_Users_Controller::get_items and noticing that there’s not an automatic way to filter via meta terms. Instead you need to apply_filters to actually input the args in the array.
So in order to achieve my searching I had to do these things.
- in functions.php or a plugin
foreach ($fields as $field) {
//register meta field
register_meta('user', $field, [
'single' => true,
'show_in_rest' => true,
'type' => 'string',
]);
//register search function in rest api
add_filter('rest_user_query', function ($args, $request) use ($field) {
if (isset($request[$field]) && !empty($request[$field])) {
$args['meta_query'][] = [
'relation' => 'AND',
[
'key' => $field,
'value' => $request[$field],
'compare' => '='
]
];
}
return $args;
}, 10, 2);
}
- In the rest request just add the params
[
'trdr' => $trdr
]
Email can be searched like this
[
'search' => $email,
'search_columns' => ['user_email']
]