We run a daily cron that gets all the users' data and updates their values. The problem is that get_users() is timing out because of the big userbase. Yes, I can update the php.ini file and increase the timeout limit, but it would only delay the inevitable.

So, potential solution is to get_users() in batches. For example, on the first batch, we get the users with ID 1-1000. Is the best approach to use the include parameter and list 1-1000 IDs in an array? Seems quite desperate.

What other options are there for scenarios like these?


You are on the right track with the batch idea. You'll need a combination of parameters in your get_users() call. A combination of the "number" and "offset" parameters should get you what you need. In the example below $count is a reference to the batch that's currently running.

// inside a loop where $count is the number of times the loop has run
$args = array(
    'number'       => 1000,
    'offset'       => $count * 1000,

UPDATE: $count would start at 0.

The first time through the loop you'd get a set of 1000 users starting from the first user in the database because 0 * 1000 = 0 (no offset)

The second time through the loop you'd get another set of 1000 users starting from the 1001st user in the database because 1 * 1000 = 1000 (i.e. skip the first 1000 results and retrieve the next set)

Your loop should continue for as long as you have results being returned.

  • Help me understand this. So $count is the number of times the loop as run, so it starts with 1. I understand that part. But exactly which users does it get in each batch? For example, I set $count to 1, and the results included users with id 10 and id 5000... as you can understand, I want to ensure that all users will be covered once the batch is finished.
    – Christine Cooper
    Sep 30 '15 at 14:48
  • No problem at all. Good luck with your project.
    – dswebsme
    Sep 30 '15 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.