admin管理员组

文章数量:1278946

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:


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:

[per_page]=0

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

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

Share Improve this question asked Sep 30, 2021 at 8:55 A.M.P.A.M.P. 31 silver badge2 bronze badges 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 Commented Sep 30, 2021 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. Commented Sep 30, 2021 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 Commented Sep 30, 2021 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. Commented Sep 30, 2021 at 11:36
Add a comment  | 

2 Answers 2

Reset to default 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/rest-api/using-the-rest-api/pagination/

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.

本文标签: Retriving all users with REST API not working