I have tried the following two approaches, each resulting that the cronjob gets added over and over again, until I remove the code. I literally had it scheduled hundreds of times…
if(!wp_next_scheduled('send_order_surveys')){
wp_schedule_event(time(), '30min', 'send_order_surveys');
}
if(!wp_get_schedule('send_order_surveys')){
wp_schedule_event(time(), '30min', 'send_order_surveys');
}
What am I doing wrong?
Well, I found the answer in the actual documentation:
https://developer.wordpress.org/reference/functions/wp_next_scheduled/
in form of a comment by “ub3rst4r”:
Note the $args parameter! Not specifying the $args parameter in wp_next_scheduled but having $args for wp_schedule_event will cause many events to be scheduled (instead of just one).
Bad Example:
if ( ! wp_next_scheduled( 'myevent' ) ) { // This will always be false
wp_schedule_event( time(), 'daily', 'myevent', array( false ) );
}
Good Example:
$args = array( false );
if ( ! wp_next_scheduled( 'myevent', $args ) ) {
wp_schedule_event( time(), 'daily', 'myevent', $args );
}