【问题标题】:Update post meta not firing on transition post status更新帖子元未在转换帖子状态时触发
【发布时间】:2020-02-12 04:40:12
【问题描述】:

每当发布新产品(要发布的草稿)时,我都会尝试添加 woocommerce 销售日期。

我的所有代码都根据我的评论运行,所有变量都可以正常显示,没有错误,但是当我检查帖子元数据和管理员时,没有添加任何日期。 get_post_meta 中没有任何内容,产品编辑屏幕中也没有任何内容。

add_action('transition_post_status', array($this, 'knp_add_sale_date'), 20, 3);
public function knp_add_sale_date($new, $old, $post){

    $meta = get_post_meta($post->ID);
    $submissiondata = $this->get_submission($meta['submissionID'][0]);
    $submissiondata['live_status'] = $new;

    //If the post is a new product published from draft. 
    if ($old == 'draft' && $new == 'publish' && $post->post_type == 'product') {    

        //If there is a sale on then set the sale dates
        if (isset($meta['_sale_price'][0])) {   
            $startdatedate = new DateTime('now');
            $startdateformat = $startdatedate->format('Y-m-d');
            $enddate = date('Y-m-d', strtotime($startdateformat. ' + 15 days'));
            $finishdate = new DateTime($enddate);       

            //Everything works till here
            update_post_meta($post->ID, '_sale_price_dates_from', $startdatedate->getTimestamp());
            update_post_meta($post->ID, '_sale_price_dates_to', $finishdate->getTimestamp());

        }   
        $update = $this->post_submission($submissiondata, $meta['submissionID'][0], 'live');                                            

    }       

}

TIA 寻求任何建议或帮助。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果您打算只访问一个元数据,您应该传递元数据键以过滤掉它的确切值。

    $meta = get_post_meta($post->ID);
    $meta = get_post_meta( $post->ID, '_sale_price', true);
    

    当您更改挂钩'transition_post_status' 此操作时,如果设置了“_sale_price”,还请检查此帖子 ID 的 postmeta 表。

    这个钩子在 post_save 之前应用到数据库,如果 meta 没有保存,那么如果它没有被设置,你就无法从数据库中获取值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多