WP cron: when to schedule a 1-time event

If I schedule the cron job to run “immediately” (as in the code
above), can I be assured that by the time cron_func() is called that
ALL of the DB updates related to the saving of my CPT will have
completed (since the funcs called in cron_func() will need to read the
modified state of $post_id)

Yes. save_post hook is fired after the post has been actually saved. So, the cron_func will get the modified state of the post. You don’t need any alternative for this.

Are there any other considerations that I should be accounting for?
For example, what is likely the happen when:

  1. admin user A edits post X, which schedules the cron job before that
    cron job finishes,
  2. admin user B makes another edit to post X

If the cron job take some time, it’s very possible to encounter such an issue. To solve this issue, you might raise a flag(it can be a post meta) when creating the cron task and clear the flag when cron task finishes. You might want to disable editing of the post as long as the flag is present for a post.

Leave a Comment