如何根据发布日期自动更新 acf 字段?

How to auto update acf field based on posting date?

我有一个名为 Project 的自定义 post。
当 post 日期超过 7 天时,我需要用 100 自动更新一个字段。
我在 function.php 中尝试了以下男女同校,但它不会自动更新该字段。
你能告诉我如何修复代码吗?

function update_active_based_on_date() {
  // query to get all custom posts - project 
  $args = array(
    'post_type'      => 'project',
    'posts_per_page' => -1
  );
  $query = new WP_Query($args);

  // if posts are returned and more than 7days, update infosubmit field
  if ($query->have_posts()) {
  global $post;
  $timestamp = get_the_time( 'U', $post->ID );
  $diff = current_time('timestamp') - $timestamp;
    while ($query->have_posts()  &&  $diff > 604800) {
      $query->the_post();
      $field_key = "field_60836f942ae12";
      $value = "100";
      update_field($field_key, $value, $post->ID);
    } // end while have_posts
    wp_reset_postdata();
  } // end if have_posts
} // end function

谢谢。

你可以这样实现:

function update_active_based_on_date() {
    // query to get all custom posts - project
    $args = array(
        'post_type'      => 'project',
        'posts_per_page' => -1
    );
    $query = new WP_Query($args);

    if ($query->have_posts()) {
        global $post;
        while ($query->have_posts()) {
            $query->the_post();
            // if posts are returned and more than 7days, update infosubmit field
            $diff = time() - strtotime(get_the_date());
            if($diff > 604800){
                $field_key = "field_60836f942ae12";
                $value = "100";
                update_field($field_key, $value, $post->ID);
            }
        } // end while have_posts
        wp_reset_postdata();
    } // end if have_posts
} // end function

add_action('init','update_active_based_on_date');