4

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.com/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??

  • 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.org/rest-api/using-the-rest-api/… – mmm Apr 17 '18 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 Apr 17 '18 at 19:02
3

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.com/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.

  • Thanks Jordan but this doesn't address the original question - why can't we access all users and how to fix that. I appreciate the extra insight, but this answer does overcomplicated the situation for our particular use case. We'll be searching with the username, so simply running an HTTP request like http://portal.alliedbuildings.com/wp-json/wp/v2/users?search=dblackburn is much simpler and easier to troubleshoot. – MakeCodeNotWar Apr 17 '18 at 19:13
  • I understand, I am not expecting this to be accepted as the answer, but I do like to answer like this at times since I end up in stackoverflow looking for a more broad answer after hours of searching, this is more of a last resort. – Jordan Ramstad Apr 17 '18 at 19:32
1

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!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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