Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have several thousand users subrscribed to a WP website. Everyday I need to send an email to each user. The emails are custom generated depending on the data of several custom user profile fields. For each user a custom query based on this data returns a list of posts, which constitute the message of the email.

As you can imagine that can get pretty heavy on the server, for that reason, when I launch the function to parse the users, generate the email and send it, Wordpress crashes. It works fine with a couple of dozen users, but several thousand is too much.

What I'd like to do to avoid this, is to only handle ten users at a time, every two minutes for example. I can do that with a CRON function.

I'm looking for the best way to do that. It would be something like "parse the first ten users and send their email, then parse the next ten, etc."

How can I implement this ten by ten rule ? How is my function going to know on which user to start each time I call it ?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

See wp_schedule_single_event() function. The general idea would be each time after you are done with a batch of users schedule next run few minutes ahead and pass info (such as offset from which user to continue) in arguments.

On larger scale I'd consider looking into alternate more specialized solutions or simplifying your process. Mass emailing is not easy to get right and WP is not developed with such tasks in mind.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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