【发布时间】:2018-10-18 06:12:51
【问题描述】:
当您创建/更新专门属于“优惠”类型的帖子时,我正在尝试创建/更新帖子元。但是,它不会更新帖子元数据。此代码已添加到我的 functions.php 文件中。
add_filter( 'pre_post_update', 'update_voucher_deadline', 10, 2 );
function update_voucher_deadline( $post_id, $data ) {
$evergreen = get_field('offer_evergreen', $post_id);
if ($evergreen == "evergreen-yes") {
$year = date('Y');
$month = date('m');
$currentDate = "". $year . "-" . $month . "-" . date('d') . date('H') . ":" . date('i') . ":" . date('s');
$day = date("t", strtotime($currentDate));
$endOfMonth = "". $year . "-" . $month . "-" . $day . "23:59:00";
//global $post; Tried with this uncommented and also didn't work.
if ( ! add_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth)) {
update_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth);
}
}
}
【问题讨论】:
-
出现“警告:update_promo_tag_id() 缺少参数 3”是因为您的函数要求 3 个参数,但该操作在 wordpress 调用时仅提供 2 个参数 - 即: do_action( 'pre_post_update', int $post_ID, 数组 $data)
-
“无法修改标头信息 - 标头已发送”可能来自其他原因。在 PHP 已经开始构建页面内容后尝试设置标题信息时会出现此错误
-
update_post_meta 函数失败,因为您试图从传递给函数的帖子数据数组中获取帖子 ID($post 不是帖子,它是帖子数据)。改用 $post_id
-
更新的答案,不确定你对 $post_id 的意思是什么,我要替换什么。需要一些清晰度,以便我了解发生了什么变化以及为什么。你能帮忙吗?
-
更新:刚刚看到你正在使用“global $post”——这段代码有点糟糕,因为全局变量必须在函数的开头声明。改用 $post_id