WP Cron and wp_insert_post

For those who stumble upon this later, use either option 1 or 2 found here: https://core.trac.wordpress.org/ticket/19373. “For other developers who run into this and need to work around it, either of these 2 options work: call wp_set_post_terms() to add your taxonomies after calling wp_insert_post() set up a “current user” in your script before calling wp_insert_post()”

How to correct schedule my event weekly with wp_schedule_event()

According to the docs for this function, you can’t set up a weekly event by default. wp_schedule_event(int $timestamp, string $recurrence, string $hook, $args = array() ); The $recurrence value needs to be one of: “hourly” “twicedaily” “daily” You’ll need to test today’s date inside your callback function, my_event. Something like: function my_event() { # date(‘l’) … Read more

Do wp-cron scheduled tasks run asynchronously?

No, the WP-Cron tasks run asynchronously from the viewing user. They should not see any delay. Although if your task takes more than a minute, it may never finish since most hosts are setup to kill PHP processes after 30 seconds.

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 … Read more

How can I change the frequency of a scheduled event?

It looks for me that you are adding this event only when there is no such event ‘send_email_alerts_hook’ scheduled yet. Try something like this and let me know if it workded. function shedule_email_alerts() { if ( !wp_next_scheduled( ‘send_email_alerts_hook’ ) ) { wp_schedule_event(time(), ‘daily’, ‘send_email_alerts_hook’); } else { wp_reschedule_event(time(), ‘daily’, ‘send_email_alerts_hook’); } } The thing is … Read more