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

3 s
3

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;
}

Leave a Comment