Wordpress 在编辑帖子页面保存,更新帖子元数据 [英] Wordpress On Edit Post Page Save, Update Post Meta

查看:38
本文介绍了Wordpress 在编辑帖子页面保存,更新帖子元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您创建/更新特别是报价"类型的帖子时,我正在尝试创建/更新帖子元数据.但是,它不会更新帖子元.此代码添加到我的functions.php 文件中.

I am trying to create/update post meta when you create/update a post that is specifically a post type of "offer". However, it does not update the post meta. This code is added in my functions.php file.

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);
        }
    }
}

推荐答案

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

I see you use ACF plugin to create custom field which means you can use acf/save_post filter to do this something like this.
1. Check if we save post type 'offer'
2. Check if we have custom field 'offer_evergreen' with value 'evergreen-yes'
3. Check if we have custom filed 'offer_voucher_deadline' if yes - update him.
4. If we do not have custom filed 'offer_voucher_deadline' create him and save our data.

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
    }
}

这篇关于Wordpress 在编辑帖子页面保存,更新帖子元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆