WP REST API only returning partial list of users

Started working with the WP REST API for the first time.Our ultimate goal is to dynamically load UI elements based on

Started working with the WP REST API for the first time. Our ultimate goal is to dynamically load UI elements based on which user is presently logged in. However, my attempts to pull a list of all users is hitting a wall - I'm only able to return two users out of five when running the base http request:

I tried including other params like search but that will only work if the user I'm searching for is one of the two in the response I'm already getting; it doesn't return any data for the other three users whatsoever. Looking at the user profiles, there aren't any unique data points or roles that set the two users being returned apart from the other three. The two being returned are both administrators, but so is one of the three that is missing.

What on Earth am I missing??

Started working with the WP REST API for the first time. Our ultimate goal is to dynamically load UI elements based on which user is presently logged in. However, my attempts to pull a list of all users is hitting a wall - I'm only able to return two users out of five when running the base http request:

http://portal.alliedbuildings/wp-json/wp/v2/users

I tried including other params like search but that will only work if the user I'm searching for is one of the two in the response I'm already getting; it doesn't return any data for the other three users whatsoever. Looking at the user profiles, there aren't any unique data points or roles that set the two users being returned apart from the other three. The two being returned are both administrators, but so is one of the three that is missing.

What on Earth am I missing??

Share Improve this question edited Apr 17, 2018 at 19:28 MakeCodeNotWar asked Apr 17, 2018 at 17:46 MakeCodeNotWarMakeCodeNotWar 1211 silver badge8 bronze badges 2
  • 2 when no user is connected, only users with published posts are retrieved. to connect a user for a API call, look this : developer.wordpress/rest-api/using-the-rest-api/… – mmm Commented Apr 17, 2018 at 18:14
  • This doesn't appear to be the case. One of the two users showing in the response did create a post (that was deleted), but the other has had zero activity. And one of the three that isn't showing in the response has made several comments in activity feeds elsewhere on the site. I would think if this were a true statement, then any user without rights to create posts would never show in the response and that just doesn't seem correct. – MakeCodeNotWar Commented Apr 17, 2018 at 19:02
Add a comment  | 

3 Answers 3

Reset to default 11

To anyone who might still be hitting this problem, here's a checklist:

  1. Make sure you are authenticated AND your user has the list_users capability.

Example: When adding a custom role, I make sure to add the list_users capability. The user should also be logged in (what authenticated means) when making the request.

  1. By default, only users who have published posts are returned by the request. To disable this, you can remove has_published_posts from the query args, like so:

Add normally

add_filter('rest_user_query', 'remove_has_published_posts_from_api_user_query', 10, 2);
function remove_has_published_posts_from_api_user_query($prepared_args, $request)
{
    unset($prepared_args['has_published_posts']);

    return $prepared_args;
}

or within namespace

add_filter('rest_user_query', __NAMESPACE__ . '\remove_has_published_posts_from_api_user_query', 10, 2);
function remove_has_published_posts_from_api_user_query($prepared_args, $request)
{
    unset($prepared_args['has_published_posts']);

    return $prepared_args;
}

I think it depends on the specifics on what your looking for, you can gain the most control by making your own route tough, the users route is more meant to list out users, not exactly search them as there is a pretty crazy amount of stuff you could be wanting to search based off of, and doing that all through parameters might get difficult.

I am not saying it cannot be done, I just found personally that rather then fighting to get a route to follow your use case, building out custom routes is easier.

Here is an example:

// namespace is like app/v1 rather then wp/v2
register_rest_route($namespace, '/users', array(
    'methods'             => WP_REST_Server::READABLE,
    'callback'            => 'get_user_list',
    'show_in_rest' => true
));

then for the function get_user_list

function get_user_list($request) {
   //below you can change to a WQ_Query and customized it to ensure the list is exactly what you need
   $results = get_users();

   //Using the default controller to ensure the response follows the same structure as the default route
   $users = array();
   $controller = new WP_REST_Users_Controller();
   foreach ( $results as $user ) {
        $data    = $controller->prepare_item_for_response( $user, $request );
        $users[] = $controller->prepare_response_for_collection( $data );
    }

   return rest_ensure_response( $users );
}

Of course there is more too it (like pagination).

https://github/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php will show you how it builds the standard route through the get_items function. Depending on what you want to query by it could also tell you the options you need to query by (if using the default route).

The only reason I am giving such a complicated answer as it could help you learn the ins and out of how it works and what application of the REST API might suite your needs.

After much reading I found the reason why all users are not returned with the straight-forward HTTP request for WP REST API:

This is a non-authenticated request, therefore only publicly available users data is released in a GET request.

*** It's important to mention that if some data you need is unavailable, you probably need to add those fields to responses, using register_api_fields (see docs for example usage), to your user endpoints as well.

The best resource I found, which gave me this answer, was part of the detailed and easy-to-read guide on the WP REST API from Torque (see pages 35-38). Someone needs to buy that man many, many beers for writing such a great guide!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773770a4592919.html

相关推荐

  • WP REST API only returning partial list of users

    Started working with the WP REST API for the first time.Our ultimate goal is to dynamically load UI elements based on

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信