get_posts inside cron

Whenever you do get_posts or WP_Query or anything like that, it’s important to remember that the code is actually getting all of the Posts at once, and loading them from the database into memory. If you run out of memory, then your process will simply die with an error. Attempting to get very large numbers of posts at once will cause this quite often.

Turn on PHP’s display_errors. You’ll likely see an out of memory error.

Next, bump up the PHP memory_limit setting to 128M or 256M and see if you now have enough memory to do it.

Alternately, rewrite your get_posts to use pagination. Get them 100 at a time or something along those lines. Then you’re not trying to pull 1000s of posts into memory all at once.

Leave a Comment