当您创建/更新专门属于“优惠”的帖子类型的帖子时,我正在尝试创建/更新帖子元。但是,它不会更新帖子元数据。此代码已添加到我的 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);
}
}
}