Support

Account

Home Forums Backend Issues (wp-admin) Issue with update_field Not Storing Data in Custom Post Type

Solving

Issue with update_field Not Storing Data in Custom Post Type

  • Hello ACF Community,

    I’m facing an issue with updating ACF fields when duplicating pages in WordPress. Specifically, I’ve created a Custom Post Type called ‘locations’, and I’m trying to duplicate pages and update ACF fields associated with them. Unfortunately, the new data doesn’t seem to be stored in the ‘locations’ CPT.

    Use Case:
    I’m working on a site that involves a lot of locations, each as a separate page within the ‘locations’ CPT. I’m trying to duplicate existing pages (along with their ACF field data) to streamline the process of adding new locations. After duplicating a page, I want to update some ACF fields programmatically using update_field.

    Code Example:
    Here’s a simplified snippet of the code I’m using:

    // Duplicating the post (this part works fine)
    $new_post_id = wp_insert_post(array(
      'post_title'    => $original_post->post_title,
      'post_type'     => 'locations',
      'post_status'   => 'publish'
    ));
    
    // Updating an ACF field (this is where the issue is)
    update_field('field_name', 'new_value', $new_post_id);
    

    Debugging Steps Taken:

      Confirmed that the field name/key is correct.
      Verified that the $new_post_id is for a post that is actually of the ‘locations’ CPT.
      Checked the database but didn’t find the updated field under the ‘locations’ CPT.
      Flushed and reloaded permalinks.

    Questions:

      Is there anything specific about using update_field with a Custom Post Type that I might be missing?
      Could this have something to do with how the page is being duplicated?
      Are there any known conflicts with other plugins that might be causing this issue?
      Is there a different ACF function that would be more appropriate for this use case?
      Any help or guidance would be greatly appreciated.

    Thank you!
    Paul

  • You must use the field key when you use update_field() in cases where the field does not already exist for the post, it is covered in the doc https://www.advancedcustomfields.com/resources/update_field/

  • Hi John,

    Thanks for your reply.

    I am using field keys for the update, and they update the database with the correct values, but I can’t get the values to link to the custom post type and show in Admin.

    Here’s a short version of what I’ve been doing. The whole script duplicates a number of pages and should add their respective field values.

    $new_page_id = wp_insert_post([
        'post_title' => $new_title,
        'post_name' => $new_slug,
        'post_content' => $original_page->post_content,
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_parent' => $parent_id,
      ]);
    
    if ($new_page_id) {
      update_acf_field("field_652e5e0552a46", true, $new_page_id, "Is Duplicate");
      update_acf_field("field_652e5e2e52a47", $original_page_id, $new_page_id, "Page Id"); 
    }
    
    function update_acf_field($field_key, $value, $new_page_id, $description)
    {
      $updated = update_field($field_key, $value, $new_page_id);
      error_log('-- SET ' . $value. ' - Updated: ' . ($updated ? "true" : "false"));
    }

    I’m not sure how to proceed, or whether my approach is valid.

    Any further help or guidance would be greatly appreciated.
    Thank you!
    Paul

  • Hello ACF Community,

    I believe I’ve found the solution on my own.

    I was using:
    add_action('publish_locations', 'duplicate_pages_for_new_location', 10, 2);

    The docs didn’t provide much low level information for new users, so I had to delve into the docs a bit more and decided to give this a try, which appears to work:
    add_action('acf/save_post', 'duplicate_pages_for_new_location');

    I hope this solution proves helpful!

    Thank you!
    Paul

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.