I have a multi-author blog, on which there are two schedule cron functions. The first function moves all public posts in the pending status after 365 days:
if( ! wp_next_scheduled( 'expire_posts_hook_publish' ) ) {
wp_schedule_event( time(), 'hourly', 'expire_posts_hook_publish' );
}
add_action( 'expire_posts_hook_publish', 'expire_posts_publish' );
function expire_posts_publish() {
global $wpdb;
$daystogo = "365";
$sql =
"UPDATE {$wpdb->posts}
SET post_status="pending"
WHERE (post_type="post" AND post_status="publish")
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query( $wpdb->prepare( $sql, $daystogo ) );
}
The second function moves the post pending in the trash ( I have trash set to 0 days, then permanently delete ) after 395 days. If they are not edited or updated:
if ( ! wp_next_scheduled( 'expire_posts_hook' ) ) {
wp_schedule_event(time(), 'hourly', 'expire_posts_hook' );
}
add_action( 'expire_posts_hook', 'expire_posts' );
function expire_posts() {
global $wpdb;
$daystogo = "395";
$sql =
"UPDATE {$wpdb->posts}
SET post_status="trash"
WHERE (post_type="post" AND post_status="pending")
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query( $wpdb->prepare( $sql, $daystogo ) );
}
I would like to perfect this function because I am afraid that if while editing the post and you activate the cron simultaneously, generates an error.
In the sense that while you edit the post, and simultaneously activates the cron function, after the publish or after update, it generates an error message about the post that no longer exists
Any ideas?