在 save_post 上,如果此 meta_key 不存在,我想在帖子中添加到期日期(现在 + 30 天),否则什么也不做。
我在我的function.php中尝试了这段代码:
add_action( 'save_post', 'update_date' );
function update_date( $post_id ) {
$expire = date( 'm/d/Y H:i:s', strtotime( '+' . '30' . ' days' ) );
$meta_exist = get_post_meta($post_id, 'expire_date', true);
if ($meta_exist == ''){
add_post_meta( $post_id, 'expire_date', $expire, true );
}
}
但我注意到,这样它总是更新日期,如果它已经定义了。
如何仅在需要时添加日期?