admin管理员组

文章数量:1313317

I have Advanced Custom Field “expired_date” of DATE type. I want to change the post category after the date is passed. I’m using this code:

function set_expiry_date( $post_id ) {

  // See if an expired_date has been entered and if not then end the function
  if( get_post_meta( $post_id, $key = 'expire_date', $single = true ) ) {

    // Get the end date of the event in unix grenwich mean time
    $acf_end_date = get_post_meta( $post_id, $key = 'expire_date', $single = true );

  } else {

    // If expired_date not set. Lets delete any CRON jobs related to this post and end the function.
    wp_clear_scheduled_hook( 'closed', array( $post_id ) );
    return;

  }

  // Convert our date to the correct format
  $unix_acf_end_date = strtotime( $acf_end_date );
  $gmt_end_date = gmdate( 'Ymd', $unix_acf_end_date );
  $unix_gmt_end_date = strtotime( $gmt_end_date );

  // Get the number of seconds in a day
  $delay = 24 * 60 * 60; //24 hours * 60 minutes * 60 seconds

  // Add 1 day to the end date to get the day after the event
  $day_after_event = $unix_gmt_end_date + $delay;

  // If a CRON job exists with this post_id them remove it
  wp_clear_scheduled_hook( 'closed', array( $post_id ) );
  // Add the new CRON job to run the day after the event with the post_id as an argument
  wp_schedule_single_event( $day_after_event , 'closed', array( $post_id ) );

}

// Hook into the save_post_{post-type} function to create/update the cron job everytime an event is saved.
add_action( 'acf/save_post', 'set_expiry_date', 20 );

// Create a function that adds the post to the Closed category
function set_past_event_category( $post_id ){

  // Set the post category to 'Past Event'
    wp_set_post_categories( $post_id, array( 5 ), true );
    wp_remove_object_terms( $post_id, 'active', 'category' );

}

// Hook into the closed CRON job so that the set_past_event_category function runs when the CRON job is fired.
add_action( 'closed', 'set_past_event_category' );

Everything was working perfectly until the past couple of days. I’m assuming it is related to the WordPress upgrade. I’m testing cron events with WP Crontrol plugin. When the post is updated, sometimes the cron event disappears. Sometimes it cannot be updated. It is very weird.

Does anybody see what is wrong?

Thanks.

本文标签: phpCron events are disappearingor cannot be updated in WordPress