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 |2 Answers
Reset to default 1The 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
版权声明:本文标题:Retriving all users with REST API not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292987a2370655.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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