If you look at the documentation, first_name
is not in the list of accepted values for the fields
parameter, and if you enabled debugging, you’d see adding first_name
to that parameter would actually cause a database error — Unknown column 'wp_users.first_name' in 'field list'
. And the same goes to the “firstname” and any other non-standard values.
So as the documentation says, “You must create a second query to get the user meta fields by ID or use the __get
PHP magic method to get the values of these fields.“, you would need to manually add the first_name
item to your $members
array like so:
// &$member means we're modifying the original $members array
foreach ( $members as &$member ) {
$member->first_name = get_user_meta( $member->ID, 'first_name', true );
}
But make sure ID
is in the field list, e.g. $user_fields = array( 'user_nicename', 'user_url', 'ID' );
.
And as for controlling the number of results, check the pagination parameters like number
:
// Get at most 10 users (per page).
$user_query = new WP_User_Query( array( 'number' => 10 ) );
And BTW, remember to set permission_callback
for your custom REST API endpoint.