[wp-hackers] get_users() exhausts memory with 'all_with_meta' argument

Otto otto at ottodestruct.com
Fri Jan 3 18:36:17 UTC 2014

On Fri, Jan 3, 2014 at 12:15 PM, Jeremy Clarke <jer at simianuprising.com> wrote:
> The question is interesting because it seems like it really applies to any
> plugin that might want to fetch all users with meta.

Realistically, there's no good reason to attempt to load all users
with all the meta all at once anyway. You should limit your queries to
something more sane.

Examine your specific case, and take action accordingly. The original
post mentioned the Email Users plugin. So unless you're actually
retrieving all the users to email them all at once, then don't call
get_users() without some limiting parameters.

The get_users() function is basically a wrapper for WP_User_Query,
which can take a number of limiting parameters. If you're needing to
page through users for display or selection, then it has a "number"
and "offset" field that can be used for paging, like it does for the
users list. Or, if you need to retrieve users that only have a
specific meta_key to allow them to be emailed (like an opt-in), then
you can send it a combination of meta_key/meta_value/meta_compare
parameters to limit the query to only return those users.

I can't think of any real case where you actually need not just all
the users, but all their meta too. You will certainly need some subset
of users, or all the users but without the need for the meta, or you
might need to page through them and get some of the meta about them
too. As long as you're careful to limit your requests to only that
which is needed, you will have far fewer problems.


More information about the wp-hackers mailing list