I try to create a scheduled cron job which runs every hour, Its my first time working with wp-cron.
In the cron function, I want to update post meta values, if some conditions are met.
I also tested the code of the cron function outside of the cron to see/print the results. The results looked ok, but when the cronjob runs no post gets updated.
Iam using WP Crontrol to see the all available cronjobs.
First I schedule an event (this seems to works):
function prfx_hourly_status_update_cron_job() {
if ( ! wp_next_scheduled( 'prfx_run_hourly_status_update_cron_job' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'prfx_run_hourly_status_update_cron_job' );
}
}
add_action( 'wp', 'prfx_hourly_status_update_cron_job' );
After creating this event I than try to run this function.
I get all posts which have a meta field where the value is not empty.
This field is an date. (Y-m-d)
For each post/product I than get two meta values.
The deadline-date and a sooner deadline-soon-date.
I than compare the todays date with these saved dates and want to update another meta value based on this.
function prfx_run_hourly_status_update_cron_job( ) {
// create meta query to get all posts which have a deadline date (field not empty)
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_prfx_custom_product_deadline',
'value' => '',
'compare' => '!='
),
),
'post_type' => 'product',
'no_found_rows' => true, //speeds up a query significantly and can be set to 'true' if we don't use pagination
'fields' => 'ids', //again, for performance
);
$posts_array = get_posts( $args );
// start if we have posts
if ($posts_array) {
// get todays date in Y-m-d format
$today = date('Y-m-d');
//now check conditions and update the code
foreach( $posts_array as $post_id ){
$saved_deadline = get_post_meta( $post_id, '_prfx_custom_product_deadline', true ); // get deadline-date
$soon_deadline = get_post_meta( $post_id, '_prfx_custom_product_deadline_soon', true );// get deadline-soon-date
if ($today > $saved_deadline) { // if today is after deadline
update_post_meta( $post_id, '_prfx_custom_product_status', 'deadline-met' );
} elseif ($today < $saved_deadline && $today > $soon_deadline) { // if today is before deadline but after soon-deadline
update_post_meta( $post_id, '_prfx_custom_product_status', 'deadline-soon' );
}
}
}
}
As said above I tried to dry run this function, instead of update_post_meta I just printed the data out. This seems to work. Iam also using a similar function to echo out some strings on the frontend, so the date compare also works.
Update: So, I cant get this to work with the cronjobs. I just tested the following code inside an plugin, as I activate the plugin, the code runs.
The query is working fine, I printed/checked the results, and it is working as simple plugin code.
But the same code is not running in the cronjob. When I look at the cron output with Wp Crontrol, I can see the hourly event “prfx_run_hourly_status_update_cron_job”, but without any action assigned. However, for example “woocommerce_geoip_updater” has also no action listed.
Maybe someone has an idea why?
Here is the tested code:
add_action('init','prfx_resave_posts');
function prfx_resave_posts(){
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_prfx_custom_product_deadline_soon',
'value' => '',
'compare' => '!='
),
),
'no_found_rows' => true, //speeds up a query significantly and can be set to 'true' if we don't use pagination
'fields' => 'ids', //again, for performance
);
$posts_array = get_posts( $args );
if ($posts_array) { //output: Array ( [0] => 120399 [1] => 120431 [2] => 120469 [3] => 120401 [4] => 120433 )
// get todays date in Y-m-d format
$today = date('Y-m-d');
foreach ($posts_array as $my_post) {
#print_r($my_post);
//output example: 120399
$saved_soon_deadline = get_post_meta( $my_post, '_prfx_custom_product_deadline_soon', true );
#print_r($saved_soon_deadline);
//output: 2018-11-30
if ($today > $saved_soon_deadline) {
update_post_meta( $my_post, '_prfx_custom_product_status', 'my-new-val-here' );
}
}
}
}