在新/编辑的自定义帖子类型帖子后添加/更新帖子元

时间:2017-01-06 16:50:09

标签: php wordpress woocommerce

WordPress 4.7 中,当管理员创建或更改 WooCommerce时,我正在尝试添加/更新帖子仪表板中的产品

我使用动作save_post_post_type上的函数执行此操作,以便在WP3.7 +中使用:https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

这是我的功能:

    function my_function() {
        update_post_meta($post_id, '_gravity_form_data', 'a:15:{s:2:"id";s:1:"3";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:2:"no";s:22:"disable_label_subtotal";s:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"disable_anchor";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";s:8:"use_ajax";s:2:"no";}');
    }
    add_action( 'save_post_product', 'my_function', 10 );

自定义帖子类型(由WooCommerce设置)为产品

我似乎无法解决这个问题。我确实设法在创建(不编辑)产品时触发了某些操作但我已经修改了代码太多次以进行回溯。

我尝试过使用瞬态,add_post_meta,wp_insert_post等。

1 个答案:

答案 0 :(得分:0)

我通过使用wp_insert_post(适用于新/编辑)并在条件中检查帖子类型来修复此问题。

副作用是我尝试添加序列化数据并update_post_meta序列化数据。

相关问题