【发布时间】: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