0

I am trying to list registered users on the frontend which have a role of 'member'. I have a lot of advanced custom fields for the users though and don't know how to get those fields. I installed the 'AFC to rest' plugin which gave me an endpoint which had all the advanced custom fields for the user but no the standard ones like first name, last name etc.

So, I don't know if it would be better to forget the ACF to rest plugin and do it differently or use the plugin but somehow get the standard user fields. Currently I am creating my own route:

add_action( 'rest_api_init', 'rest_members_endpoint' );
function rest_members_endpoint() {
    $namespace = 'rest/v1/';

    register_rest_route( $namespace, 'members/', array(
        'methods'   => 'GET',
        'callback'  => 'list_members',
    ) );
}


function list_members( $request ) {
$params = $request->get_params();

    $args = array(
        'role'    => 'member',
    );

$wp_user_query = new WP_User_Query( $args );
$members = $wp_user_query->get_results();

    return new WP_REST_Response(array('members' => $members), 200);
}

The ajax:

$.ajax({
  method: 'GET',
  url: mySiteData.api_url + 'members/',
  beforeSend: function ( xhr ) {
      xhr.setRequestHeader( 'X-WP-Nonce', mySiteData.nonce );
  },
})

This gives me back ID, display name, user_email, user_nicename but there is not first name and last name, and no Advanced custom fields.

1 Answer 1

0

Once you get the id of the user in members variable. You then have to use get_post_meta to get the custom data of members because ACF uses meta boxes. After that merge the data with the members variable and then pass the variable to WP_REST_Response class. I would have commented but that reputation, so posted answer, Hope you don't mind and hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.