I used wp_schedule_event() to schedule a task to run hourly on my website. For some reason, the function i want to run is working, the event itself appear under the 'cron' option table. Everything seems fine but when i wait and check if the functions did run i have nothing.
i use hourly for testing but it's meant to be daily
Here is the full snippet of php code :
/*
======================================================
MANAGE SUB CANCELLATIONS
1 - daily event to run
2 - manage_sub_cancel()
======================================================
*/
/* *
*
* -- > 1 - daily event to run
*
* */
add_action( 'wp', 'check_sub_activation' );
function check_sub_activation() {
if ( ! wp_next_scheduled( 'check_sub_event' )) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'check_sub_event' ); // creating the hook
}
}
// add_action( 'wp', 'check_sub_deactivation');
function check_sub_deactivation() { // will clear the scheduled hook if activated
wp_clear_scheduled_hook('check_sub_event');
}
/* *
*
* -- > 2 - manage_sub_cancel()
*
* */
function manage_sub_cancel() { // This function will apply the cancellation of a boutique when period end is reached
$args = array(
'post_type' => 'boutique',
'meta_key' => 'cancel_at_end',
'meta_value' => 'true',
'meta_compare' => '=',
);
$the_query = new WP_Query( $args );
$boutique_list = $the_query->posts;
foreach ( $boutique_list as $boutique ) {
$boutique_id = $boutique->ID;
$period_end = get_post_meta( $boutique_id, 'period_end', true );
$today = time();
if ( $today < $period_end ) { // À INVERSER LORSQUE TERMINÉ
update_post_meta( $boutique_id, 'sub_id', "" );
update_post_meta( $boutique_id, 'pckg_id', "1" );
update_post_meta( $boutique_id, 'cancel_at_end', "false" );
update_post_meta( $boutique_id, 'period_end', "" );
} else {
// Period end is not passed
}
}
}
add_action( 'check_sub_event', 'manage_sub_cancel' ); // add manage_sub_cancel() function to 'check_sub_event' hook we just created
I'm also using the plugin Advanced Cron Manager to list all cron jobs, i do see the one i created here, i can also "execute now" and it's working.. but when i wait for the even to run every hour i don't see anything even if i visit the website. I don't know it seems like everything is done fine but nothing happen.