【发布时间】:2021-09-02 04:32:06
【问题描述】:
我正在尝试在发布新帖子/产品时设置产品的 menu_order 值,如下所示:
function set_post_to_last_order($post_id, $post_after, $post_before) {
if (get_post_type($post_id) === 'product'):
if (($post_before->post_status === 'auto-draft' || $post_before->post_status === 'draft') && $post_after->post_status === 'publish'):
$product = new WC_Product($post_id);
$products = new WP_Query([
'post_type' => 'product',
'post_status' => 'publish'
]);
$product->set_menu_order($products->post_count + 1);
endif;
endif;
}
该函数在 post_updated 操作上运行。
add_action('post_updated', 'set_post_to_last_order', 100, 3);
它实质上将菜单顺序设置为比产品总数多 1。该函数正确运行并设置菜单顺序,但是,它被帖子中自定义字段中的表单值覆盖,默认情况下,新产品设置为 0。我希望 post_updated 在 之后触发 em> 字段不是以前保存的。有没有办法让函数在 $_POST 请求在发布/保存时运行后运行?
【问题讨论】:
-
set_menu_order调用$this->set_prop,我不确定这是否真的写入数据库 - 或者只是更新 PHP 对象实例的属性。我想你之后可能需要显式调用save方法……? -
@CBroe 我将如何保存它?
-
通过调用
save方法...? -
@CBroe 是的,这是我很久以来问过的最愚蠢的问题。
-
@CBroe Legend,这是一种享受。毕竟。
标签: php wordpress woocommerce hook-woocommerce