This repository has been archived by the owner. It is now read-only.

WP Users Endpoint doesn't return all users #2300

Closed
robynlarsen opened this Issue Feb 22, 2016 · 12 comments

Comments

Projects
None yet
8 participants
@robynlarsen

robynlarsen commented Feb 22, 2016

Has anyone run into the issue where not all the users are returned? At the moment only users who have contributed to content are returned in the following endpoint. At the moment there should be 15 users returned and only 6 are included in the following request:
http://dev-twg-express.pantheon.io/wp-json/wp/v2/users

Am I missing something?

Have also tried forcing http://dev-twg-express.pantheon.io/wp-json/wp/v2/users?filter[per_page]=-1

Thanks so much for all your help.

@danielbachhuber

This comment has been minimized.

Member

danielbachhuber commented Feb 22, 2016

At the moment only users who have contributed to content are returned in the following endpoint.

This is intentional. If a user hasn't published a post, then they won't be included in the API response. See #1397, #2155

@robynlarsen

This comment has been minimized.

robynlarsen commented Feb 22, 2016

@danielbachhuber is there a filter that will pull in all the users?

@danielbachhuber

This comment has been minimized.

Member

danielbachhuber commented Feb 22, 2016

You can use the rest_prepare_users filter to remove has_published_posts from the query args https://github.com/WP-API/WP-API/blob/develop/lib/endpoints/class-wp-rest-users-controller.php#L126

@robynlarsen

This comment has been minimized.

robynlarsen commented Feb 22, 2016

@danielbachhuber stupid question - you mean to use that on the WP side of things instead of within the endpoint?

@danielbachhuber

This comment has been minimized.

Member

danielbachhuber commented Feb 22, 2016

stupid question - you mean to use that on the WP side of things instead of within the endpoint?

Yes, you'll need to add the filter in PHP.

@robynlarsen

This comment has been minimized.

robynlarsen commented Feb 22, 2016

For anyone looking to solve this problem:

API Request /users?per_page=0
Note: per_page=-1 only served up the 10 users (current limit).

Additional function in your php theme.

class UserFields {

 function __construct() {
  add_filter('rest_user_query',           [$this, 'show_all_users']);
 }

function show_all_users($prepared_args) {
    unset($prepared_args['has_published_posts']);

    return $prepared_args;
  }
}

new UserFields();
@robynlarsen

This comment has been minimized.

robynlarsen commented Feb 22, 2016

@danielbachhuber thanks for your help 👍

@mrbm

This comment has been minimized.

mrbm commented Apr 23, 2017

Looks like this is still occurring....to my morning displeasure. As more people attempt to use wordpress as a backend for their apps, many of the users wont have "posts" Doesn't seem to make sense for the query to display users really be a query to display all users with "posts" and to not be able to retrieve a user unless the user has a post....this is how it is set up in 4.7.4 as of writing this.

@kadamwhite kadamwhite reopened this Apr 23, 2017

@kadamwhite kadamwhite closed this Apr 23, 2017

@timothyjensen

This comment has been minimized.

timothyjensen commented May 4, 2017

I can see the logic in returning users who have published content (posts, pages, and attachments) by default. It's easy enough modify the query args with the filter that Daniel pointed to. For those more comfortable with procedural code, here an alternative to @robynlarsen 's code posted above:

add_filter( 'rest_user_query', 'prefix_remove_has_published_posts_from_wp_api_user_query', 10, 2 );
/**
 * Removes `has_published_posts` from the query args so even users who have not
 * published content are returned by the request.
 *
 * @see https://developer.wordpress.org/reference/classes/wp_user_query/
 *
 * @param array           $prepared_args Array of arguments for WP_User_Query.
 * @param WP_REST_Request $request       The current request.
 *
 * @return array
 */
function prefix_remove_has_published_posts_from_wp_api_user_query( $prepared_args, $request ) {
	unset( $prepared_args['has_published_posts'] );

	return $prepared_args;
}
@Fi3

This comment has been minimized.

Fi3 commented Aug 10, 2017

This is not very clear in the docs. Could you please update the api ref? :)

@b4dnewz b4dnewz referenced this issue Feb 19, 2018

Merged

Various updates #26

@sepion02

This comment has been minimized.

sepion02 commented Apr 24, 2018

Thanks @timothyjensen.
Unfortunatly this is not working for a single user request ( "/wp/v2/users/12" for example )
filter is not fired.
Any idea ?

@pablito-ernesto

This comment has been minimized.

pablito-ernesto commented May 29, 2018

In the get_item function none filter is applied, so the best option for me is the API filtering results by slug.
I use wp-json/wp/v2/users/?slug=han-solo -> this request is solve in get_items.
instead of
wp-json/wp/v2/users/1 -> this request is solve in get_item, so none filter was applied.
I hope helped you !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.