You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress开发问题:自定义文章类型保存后同步WooCommerce产品ACF字段异常

Fix: ACF Field Sync Issues When Syncing Custom Post Type to WooCommerce Product

You’re spot-on about the root cause here — the save_post_award_category hook fires during the post save process, before ACF has finished writing its field values to the database. That’s why you’re getting empty values on first create, and old values on updates.

The solution is to use ACF’s dedicated acf/save_post hook instead, with a higher priority to ensure all field data is fully persisted before your sync logic runs. I’ve also fixed a few other small issues in your original code (like undefined $product_id during updates and redundant post variable overwrites):

function sync_award_category_to_product($post_id) {
    // Only run for our custom post type
    if (get_post_type($post_id) !== 'award_category') {
        return;
    }

    // Skip autosaves and revisions to avoid unnecessary syncs
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
        return;
    }

    // Grab the latest post data
    $post_title = get_the_title($post_id);
    $post_slug = get_post_field('post_name', $post_id);

    // Check if a matching WooCommerce product already exists
    $existing_product = get_page_by_path($post_slug, OBJECT, 'product');

    if ($existing_product) {
        $product_id = $existing_product->ID;
        // Ensure the product is published (adjust as needed)
        wp_update_post(array(
            'ID' => $product_id,
            'post_status' => 'publish'
        ));
    } else {
        // Create a new published product
        $product_id = wp_insert_post(array(
            'post_title' => $post_title,
            'post_type' => 'product',
            'post_status' => 'publish',
            'post_name' => $post_slug
        ));
    }

    // Now safely fetch the latest ACF field values
    $award_description = get_field('description', $post_id);

    // Sync the description field to the product
    if (!empty($award_description)) {
        update_field('description', $award_description, $product_id);
    }
}
// Use ACF's save_post hook with priority 20 (ACF runs at 10 by default)
add_action('acf/save_post', 'sync_award_category_to_product', 20);

Key Improvements:

  • Hook Timing: acf/save_post with priority 20 ensures ACF has already saved all field data to the database, so you’ll always get the latest values.
  • Safety Checks: We skip autosaves/revisions and only target our custom post type to prevent unintended behavior.
  • Proper Product ID Handling: The original code left $product_id undefined when updating an existing award category — this fixes that.
  • Cleaner Field Fetching: Instead of pulling all fields, we grab just the one we need (you can add more fields by repeating the get_field/update_field pattern).
  • Non-Empty Check: Avoids writing empty values to the product’s description field.

Quick Notes:

  • Double-check that the ACF field key description exists on your WooCommerce product post type — if your product uses a different field key, update that in the update_field call.
  • If you need to sync additional ACF fields (like images, prices, etc.), just add more get_field calls and map them to the corresponding product fields.
  • For WooCommerce-specific product settings (like product type, stock status), you can use update_post_meta instead of ACF fields if needed.

内容的提问来源于stack exchange,提问作者Daniel Kilburn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:42:31