【问题标题】:Wordpress On Edit Post Page Save, Update Post MetaWordpress 在编辑帖子页面保存,更新帖子元
【发布时间】: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

标签: php wordpress post-meta


【解决方案1】:

我看到您使用 ACF 插件创建自定义字段,这意味着您可以使用 acf/save_post 过滤器来执行类似这样的操作。
1.检查我们是否保存帖子类型'offer'
2. 检查我们是否有自定义字段“offer_evergreen”,值为“evergreen-yes”
3. 检查我们是否有自定义提交的“offer_voucher_deadline”,如果是 - 更新他。
4. 如果我们没有自定义归档“offer_voucher_deadline”,请创建并保存 我们的数据。

add_filter('acf/save_post', 'update_voucher_deadline', 20);
function update_voucher_deadline($post_id) {
    if ( get_post_type($post_id) != 'offer' ) //if current post type not equal 'offer' return
        return;

         $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";

    if ( get_field('offer_evergreen') == 'evergreen-yes' ) {

        if ( get_post_meta( $post_id, 'offer_voucher_deadline', true ) ) //If get post meta with key 'offer_voucher_deadline' - update meta
            update_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth);
        else //else if do not have post meta with key 'offer_voucher_deadline' create post meta
            add_post_meta( $post_id, 'offer_voucher_deadline', $endOfMonth);

    } else {
        return; //Remove return and add what you want to save, if offer_evergreen not equal to evergreen-yes
    }
}

【讨论】:

  • 你太棒了,我忘记了 acf 钩子,因为我以前从未使用过它。非常感谢!
  • 现在当 evergreen 不是 == yes 时,它不会将值保存为 offer_voucher_deadline 的常规字段
  • 如果 evergreen 不是 == yes,则可以将 else 后面的第二个 return 替换为要保存的内容
  • 我太累了,截止日期字段已经更新,所以即使我在 else 语句中添加了正确的代码,它总是会显示与之前设置的截止日期相同...谢谢帮助!
  • 您可以短代码也不需要检查 get_post_meta() 是否存在或不使用两个时间 update_post_meta() 选项/如果不存在则自动创建值。
【解决方案2】:

首先pre_post_update钩子不会在创建时触发,它会在现有帖子更新之前立即触发。

您需要使用save_post 挂钩,每当创建或更新帖子或页面时都会触发它

add_action( 'save_post', 'update_voucher_deadline', 10, 3 );
/**
 * Save post metadata when a post is saved.
 *
 * @param int $post_id The post ID.
 * @param post $post The post object.
 * @param bool $update Whether this is an existing post being updated or not.
 */
function update_voucher_deadline( $post_id, $post, $update ) { 
    $evergreen = get_field('offer_evergreen');
    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 参数更新您的函数。

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多