
本文旨在解决在 WordPress 中,当特定类型的文章(例如 "award_category")新增或更新后,需要自动创建或更新 WooCommerce 产品的问题。通过使用合适的 WordPress 钩子,确保在文章数据完全保存后执行相关函数,从而避免数据不一致的问题。本文将介绍两种可行的解决方案,并提供详细的代码示例和注意事项。
wp_after_insert_post 是 WordPress 5.6.0 版本新增的钩子,它会在文章及其关联的 terms 和 meta 数据保存完成后触发。这使得它成为在文章保存后执行自定义逻辑的理想选择,例如创建或更新相关的 WooCommerce 产品。
代码示例:
/**
* 在 "award_category" 文章保存后创建/更新 WooCommerce 产品。
*
* @param int $post_id 文章 ID。
* @param WP_Post $post 文章对象。
* @param bool $update 是否为更新现有文章。
* @param WP_Post $post_before 更新前的文章对象,新建文章为 null。
*/
function save_award_category( $post_id, $post, $update, $post_before ) {
if ( get_post_type( $post_id ) == 'award_category' ) {
// 获取文章对象
$post = get_post( $post_id );
// 检查是否存在同名产品
$product = get_page_by_path( $post->post_name, OBJECT, 'product' );
// 如果不存在同名产品
if ( !$product ) {
// 创建产品文章
$product_id = wp_insert_post(array(
'post_title' => get_the_title($post->ID),
'post_type' => 'product',
'post_status' => 'publish'
));
} else {
$product_id = $product->ID;
}
// 获取文章的 ACF 字段组数组
$award_category_fields = get_fields( $post->ID );
// 确保 $award_category_fields 是一个数组且包含 'description' 键
if (is_array($award_category_fields) && isset($award_category_fields['description'])) {
// 更新产品描述,使用文章描述
update_field( 'description', $award_category_fields['description'], $product_id );
}
}
}
add_action( 'wp_after_insert_post', 'save_award_category', 10, 4 );代码解释:
注意事项:
updated_post_meta 钩子会在特定类型的文章的元数据更新后触发。 虽然这个钩子是针对元数据的,但可以用来确保在所有 ACF 字段(包括描述)更新后执行操作。
代码示例:
/**
* 在 "award_category" 文章元数据更新后创建/更新 WooCommerce 产品。
*
* @param int $meta_id 已更新元数据的 ID。
* @param int $post_id 文章 ID。
* @param string $meta_key 元数据键。
* @param mixed $meta_value 元数据值。
*/
function save_award_category( $meta_id, $post_id, $meta_key = '', $meta_value = '' ) {
if ( get_post_type( $post_id ) == 'award_category' ) {
// 获取文章对象
$post = get_post( $post_id );
// 检查是否存在同名产品
$product = get_page_by_path( $post->post_name, OBJECT, 'product' );
// 如果不存在同名产品
if ( !$product ) {
// 创建产品文章
$product_id = wp_insert_post(array(
'post_title' => get_the_title($post->ID),
'post_type' => 'product',
'post_status' => 'publish'
));
} else {
$product_id = $product->ID;
}
// 获取文章的 ACF 字段组数组
$award_category_fields = get_fields( $post->ID );
// 确保 $award_category_fields 是一个数组且包含 'description' 键
if (is_array($award_category_fields) && isset($award_category_fields['description'])) {
// 更新产品描述,使用文章描述
update_field( 'description', $award_category_fields['description'], $product_id );
}
}
}
add_action( 'updated_post_meta', 'save_award_category', 10, 4 );代码解释:
注意事项:
本文介绍了两种在 WordPress 中,当特定类型的文章新增或更新后执行自定义函数的解决方案:使用 wp_after_insert_post 钩子和 updated_post_meta 钩子。 wp_after_insert_post 钩子是首选方案,因为它只会在文章及其关联数据完全保存后触发。 updated_post_meta 钩子则需要在函数中添加额外的逻辑以避免重复操作。 选择哪种方案取决于你的具体需求和 WordPress 版本。 无论选择哪种方案,都应该仔细测试代码,确保其能够正确地创建或更新 WooCommerce 产品,并且不会对网站的性能产生负面影响。
以上就是WordPress 教程:在文章新增或保存后执行函数的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号