I have a function in my functions.php file that is supossed to update my post status once the expiry date has passed. I call this function in a test.php file that is also a template page with url /test-search-results/. If I access this url via my browser the function fires and does what it supposed to do.
I want to create a server side cronjob to do this but I cant seem to get this right. I have contacted my host and they confirm that the path is correct and that the cron is executing si the problem has to be with my script.
Can someone please advise what I have to change in my script to have the cronjob execute it.
Function in my functions.php file:
function expire_posts () {
$args = array (
'post_type' => 'post',
'meta_key' => 'Expiry Date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '<',
);
$query = new WP_Query($args);
if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$post_data = array(
'ID' => $post_id,
'post_type' => 'post',
'post_status' => 'expired',
'post_modified' => current_time ( 'mysql' ),
);
wp_update_post($post_data);
endwhile;
wp_reset_postdata();
endif;
}
I call the function in my test.php file like this:
expire_posts();
The path my cronjob is following is:
/home/userID/public_html/wp-content/themes/twentyseventeen-child/test.php
I am hoping someone can shed some light as to why my script isn't executing via the cronjob.
I have a function in my functions.php file that is supossed to update my post status once the expiry date has passed. I call this function in a test.php file that is also a template page with url /test-search-results/. If I access this url via my browser the function fires and does what it supposed to do.
I want to create a server side cronjob to do this but I cant seem to get this right. I have contacted my host and they confirm that the path is correct and that the cron is executing si the problem has to be with my script.
Can someone please advise what I have to change in my script to have the cronjob execute it.
Function in my functions.php file:
function expire_posts () {
$args = array (
'post_type' => 'post',
'meta_key' => 'Expiry Date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '<',
);
$query = new WP_Query($args);
if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$post_data = array(
'ID' => $post_id,
'post_type' => 'post',
'post_status' => 'expired',
'post_modified' => current_time ( 'mysql' ),
);
wp_update_post($post_data);
endwhile;
wp_reset_postdata();
endif;
}
I call the function in my test.php file like this:
expire_posts();
The path my cronjob is following is:
/home/userID/public_html/wp-content/themes/twentyseventeen-child/test.php
I am hoping someone can shed some light as to why my script isn't executing via the cronjob.
Share Improve this question asked Aug 3, 2018 at 9:14 RagnaboyRagnaboy 621 gold badge1 silver badge5 bronze badges2 Answers
Reset to default 5When you use *nix cron to run a PHP file like that it won't have WordPress loaded. You can load it manually, but a better method is to use WordPress' own cron API.
Schedule the event in your functions.php
:
if ( ! wp_next_scheduled( 'expire_posts' ) ) {
wp_schedule_event( time(), 'hourly', 'expire_posts' );
}
I've used hourly
, the other available defaults are daily
& twicedaily
(more on scheduling).
Now let's hook up your expire_posts
function to our newly created expire_posts
event:
add_action( 'expire_posts', 'expire_posts' );
Tada!
NB: WordPress cron relies on a request (i.e. a user or bot visiting the site) at least as often as your schedule to run smoothly. If your site is quiet we can use *nix cron to trigger the WordPress cron API every 15 minutes and let WordPress handle its own internal events.
First, disable the default request-based cron behaviour in wp-config.php
:
define( 'DISABLE_WP_CRON', true );
Now set up a *nix cron to trigger WordPress' cron:
/15 * * * wget -q -O - http://yourdomain/wp-cron.php?doing_wp_cron
With WP cron, you can launch a action daily, twice daily or hourly :
https://codex.wordpress/Function_Reference/wp_schedule_event
If you need another period, you can define it with the filter cron_schedules
like that :
// execute the function "expire_posts" when the action "MyPlugin__cron_clean_post" is launched
add_action("MyPlugin__cron_clean_post", "expire_posts");
add_action("wp_loaded", function () {
// launch the action "MyPlugin__cron_clean_post" every 15 minutes
if (!wp_next_scheduled("MyPlugin__cron_clean_post")) {
wp_schedule_event(1, "15_minutes", "MyPlugin__cron_clean_post");
}
});
add_filter("cron_schedules", function ($cron_schedules) {
$cron_schedules["15_minutes"] = [
"display" => "15 minutes",
"interval" => 15 * MINUTE_IN_SECONDS,
];
return $cron_schedules;
});