Retrieve all users from wordpress database via REST/JSON API

Thanks to stackoverflow user Milap !

You can get all users even they have not created any post, for that you need to modify rest-api plugin.

Open wp-content/plugins/rest-api/lib/endpoints/class-wp-rest-users-controller.php file, you will find below code on line number 106,

if ( ! current_user_can( 'list_users' ) ) {
    $prepared_args['has_published_posts'] = true;
}

Change it to below,

if ( ! current_user_can( 'list_users' ) ) {
    $prepared_args['has_published_posts'] = false;
}

If you don’t want to modify plugin, put below code into current theme’s functions.php file.

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

You are done.

FYI: i choose to edit the API file and it worked.

Leave a Comment