admin管理员组

文章数量:1323335

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.

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.

Share Improve this question asked Sep 5, 2020 at 11:01 user8463989user8463989 5931 gold badge8 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: advanced custom fieldsGetting user data via ajax