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

Mike Walsh mpwalsh8 at gmail.com
Fri Jan 3 19:29:19 UTC 2014

Since I originally posted this I've been playing around with limits getting
500 users at a time.  This worked better but eventually failed as well on
the use case with 13K users.  After reading all of the replies so far, I
think it is due to the growing cache.  I added a check to watch the memory
increase with each query for 500 users and it steadily increases until
memory is exhausted.

To answer some of questions - the opt-in aspect is exactly what is stored
as meta data.  I took over this plugin a while back and this query model
has been part of it long before I started working on it.  Basically it does
a query for all users which have meta data and processes that.  The Codex
indicates the meta query has been available since 3.5.0 and this plugin
dates back to the 2.x days.

It probably makes sense to re-implement this portion of the code as it
appears pretty fragile.  One of those things that has been this way for
years and wasn't an issue because it never broke until now.


On Fri, Jan 3, 2014 at 1:36 PM, Otto <otto at ottodestruct.com> wrote:

> 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.
> -Otto
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

Mike Walsh - mpwalsh8 at gmail.com

More information about the wp-hackers mailing list