【问题标题】:How to auto update acf field based on posting date?如何根据发布日期自动更新 acf 字段?
【发布时间】:2021-04-24 02:00:34
【问题描述】:

我有一个名为 Project 的自定义帖子。
当发布日期超过 7 天时,我需要使用 100 自动更新字段。
我在 function.php 中尝试了以下 coed,但它不会自动更新字段。
请告诉我如何修复代码?

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

谢谢。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    你可以这样实现:

    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');
    

    【讨论】:

    • 嗨 Shahidiqbal:哇哇哇,它运行良好,我花了将近一周的时间来解决这个问题,但我无法解决它,你成就了我的一天,谢谢你的帮助 :),并且很抱歉回复晚了,我没有注意到你的回答,我的错。有一个安全和美好的一天 Shahidiqbal :)
    • 很高兴听到这个消息。
    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 2013-04-09
    • 2021-07-24
    • 2018-10-09
    • 2021-08-11
    • 1970-01-01
    • 2018-04-12
    • 2020-07-17
    相关资源
    最近更新 更多