0

At the moment I'm using Postman to hit the endpoints and I have a successful Basic Authentication but so far I'm unable to get all the registered users. My request:

https://example.com/wp-json/wp/v2/users

returns only users that have posts even though I have inserted:

add_filter( 'rest_user_query' , 'custom_rest_user_query' );
function custom_rest_user_query( $prepared_args, $request = null ) {
  unset($prepared_args['has_published_posts']);

  return $prepared_args;
}

in my functions file. I have also tried applying a filter:

https://example.com/wp-json/wp/v2/users?filter[per_page]=0

But I still get the same result: Only users with posts. I have been banging my head on for days now...

4
  • Can you confirm you're successfully making an authenticated request? e.g. have you done something only a logged in user could do to confirm your basic auth works? And are you aware of the pagination headers? Your filter URL is asking for zero users per page so it can never return a result
    – Tom J Nowell
    Sep 30 at 10:12
  • @TomJNowell Yes I can confirm that my requests are succesfully authenticated. I have double checked by just creating a post and it was successful. I'm not aware of paginations headers, so I will look into them. I have also tried using filter[per_page]=-1 but I'm still getting the same set of 8 users over more than 900 registered
    – A.M.P.
    Sep 30 at 11:16
  • pagination in the REST API uses 10 per page, with an upper limit of 100 for performance reasons. This is good practice outside of the REST API too but it's enforced, -1 will not give you all the results unpaged. It's likely the users you're missing are on page 2 or 3
    – Tom J Nowell
    Sep 30 at 11:30
  • 1
    @TomJNowell BINGO! YES You got it. That was my problem. Damn PAGINATION. Insisting on per_page=0 wasn't very smart and for some reason I just got stuck in that loop. Thank you very much!
    – A.M.P.
    Sep 30 at 11:36
1

The problem may be pagination

The API only returns 10 results per page, and needs follow up requests to fetch the rest. WP includes a HTTP header that tells you how many total results and the number of pages. It has a hard limit of 100 per page maximum, if you request 200, you will be capped to 100.

So your missing users may be on page 2 or 3 etc

I strongly recommend reading the REST API handbook on the official developer site, here is the page detailing pagination:

https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

0

WP has nice build in tools to work with db, my suggestion would be to register your endpoint and use for a callback function something like this:

function getAllUsers(){
    global $wpdb;
    $query = $wpdb->get_results( " SELECT `ID` FROM `wp_users` " , OBJECT);
    $ids = array();
    foreach($query as $id){
        array_push($ids, $id->ID);
    }
    return $ids;    
}

It will return all the ID's. Then you can use get_post_meta to gather meta info about the user. Of course consider protecting your endpoint since it hands over user information.

5
  • the OP is hoping to use the built in endpoint
    – Tom J Nowell
    Sep 30 at 10:14
  • 1
    Thanks but as @TomJNowell said, I would like to use the built in endpoint even though this solution could be worth looking into. It's always good to learn something new.
    – A.M.P.
    Sep 30 at 11:19
  • Fair enough, that is why I mention "suggestion". Also happy to provide the rest of the code, register-endpoint etc. Sep 30 at 11:21
  • @billybadass It would be awesome if you could post the rest of the code. I have found the solution as per Tom's suggestion but I will be more than happy to look into your solution too.
    – A.M.P.
    Sep 30 at 11:39
  • Oh wow missed that it was pagination. Tom is wise have seen his answers before. My solution would be using register_rest_route and maybe get_post_meta or other custom query. I am using WP as framework but better read the documentation like Tom. Sep 30 at 11:56

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.