【问题标题】:Delete ACF Field from the backend with expired date从后端删除过期日期的 ACF 字段
【发布时间】:2017-08-03 01:40:50
【问题描述】:

我有一个 ACF 字段,称为 subseminars,在这里面是另一个称为 subseminars 的字段,它是一个包含 start_date 和 end_date 的重复字段。

我的帖子有几行该字段。

我想从后端删除包含过期日期的转发器字段的行,以便它不会显示在前端。

我正在使用此功能来实现此功能,但有些东西不起作用。

add_filter('acf/load_value/name=repeater_field_name', 'delete_old_courses_by_date');
function delete_old_courses_by_date($rows, $post_id, $field) {
  if (!is_array($value) || !count($value)) {
    return $value;
  }
  // get the current timestamp
  $now = time();

  // set up a new array to hold the values we keep
  $new_value = array();
  foreach ($rows as $row) {
    // the php strtotime() function could fail depending on
    // the return format of the date/time fields
    // this requires a valid date/time format as documented here
    // http://php.net/manual/en/datetime.formats.php
    // if this does not work I probably won't be much help figuring
    // our how to covert your return value to something usable
    $start = strtotime($row['start_date']);
    $end = strtotime($row['end_date']);
    if ($start > $now || $end > $now) {
      $new_value[] = $row;
    }
  }
  return $new_value;
}

如果我把repeater_field_name 作为start_date,所有的start_date 行都会被删除。

请不要我的日期格式是 Ymd,我不知道该格式是否与 strtotime() 函数兼容

任何帮助将不胜感激。

【问题讨论】:

  • 这玩意没人知道吗?
  • 如果您检查值中的数据,它看起来如何? var_dump($now." 必须在 ".$start." 和 ".$end);格式一样吗?
  • 它们的格式相同。 start_date 和 End_date 都是 Ymd 格式

标签: php wordpress advanced-custom-fields


【解决方案1】:

这就是最终对我有用的方法

<?php   for($i=0;$i<10;$i++){
        $ap = get_post_meta($post->ID,'sub_seminars_'.$i.'_start_date',true);
        $startdate = date("Ymd", strtotime($ap));
        $todaydate = date("Ymd");
      if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
       $del_data = array(
                    'Ref' => 'sub_seminars_'.$i.'_ref',
                    'Start date' => 'sub_seminars_'.$i.'_start_date',
                    'End Date' => 'sub_seminars_'.$i.'_end_date',
                    'Venue' => 'sub_seminars_'.$i.'_venue',
                    'Fees' => 'sub_seminars_'.$i.'_fees',
                    'CPE Credits' => 'sub_seminars_'.$i.'_cpe_credits'
        );
      delete_row('sub_seminars', 1);
    }
   } ?>

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多