Home › Forums › Backend Issues (wp-admin) › 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:
Questions:
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
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.