Cron While Editing Post

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?

1 Answer
1

It seems to have solved by changing the two functions, by taking a different approach:

/* CHECK IF CURRENT USER IS NOT IN POST.PHP AND IN POST-NEW.PHP 
AND DOWNGRADE PUBLISH POSTS IN PENDING AFTER X DAYS */

if(is_admin() ) {global $pagenow;
if( 'post.php' != $pagenow || 'post-new.php' != $pagenow) {
add_action( 'init', 'downgrade_publish_posts' );
function downgrade_publish_posts() {
$user_ID = get_current_user_id();   
global $wpdb;
$daystogo = "365";
$sql =
"UPDATE {$wpdb->posts}
SET post_status="pending"
WHERE post_type="post" 
AND post_status="publish"
AND post_author="$user_ID"
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query( $wpdb->prepare( $sql, $daystogo ) );}}}

/* CHECK IF CURRENT USER IS NOT IN POST.PHP AND IN POST-NEW.PHP 
AND DELETE PENDING POSTS AFTER X DAYS */

if(is_admin() ) {global $pagenow;
if( 'post.php' != $pagenow || 'post-new.php' != $pagenow) {
add_action( 'init', 'delete_pending_posts' );
function delete_pending_posts() {
$user_ID = get_current_user_id();   
global $wpdb;
$daystogo = "395";
$sql =
"DELETE FROM {$wpdb->posts}
WHERE post_type="post" 
AND post_status="publish"
AND post_author="$user_ID"
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query( $wpdb->prepare( $sql, $daystogo ) );}}}

Note: Obviously in my case, I have a function which forced the publication date after pending, with the current date.

Each observation, advice, and simplification is welcome.

Leave a Comment