Closed alexsoluweb closed 1 month ago
Hey there,
Updating sub fields via update_field is not supported. You'll need to update the parent field with a full array of the sub fields.
Alternatively, if you're looking to loop over each sub field to update it you can use update_sub_field inside an ACF loop.
If you'd like to raise a feature request for a new function or additional parameter to update_field, your best bet for that is to use our feedback board at https://www.advancedcustomfields.com/feedback - as that's where we collate upvotes which influences our product roadmap.
Cheers! Liam
Well thanks for your response, but my real issue is actually this:
The acf_form() function is producing incorrect output when rendering multiple field groups on the same page that share the same field name structure. Specifically, a form field is receiving the field_key of another field form, resulting in one field having an empty value.
This project requires debugging, and unfortunately, it was not my design. The existing codebase is in a poor state, making it challenging to manage. However, I need to find a workaround without refactoring the entire project.
Hey @alexsoluweb,
Yeh, ACF doesn't support multiple fields of the same name on the page at the same time, names must be unique per save.
It's perfectly viable to have multiple fields with the same name when only one is displayed due to conditional logic, but if you try to show two at once, it's only going to save the last value.
This isn't something we can fix easily, and it would likely cause more problems than it solved if we did; just like you can't store multiple meta items with the same name in the core WordPress custom fields functionality too - for that reason, I'm going to close this issue.
You'll need to change your field groups to use different names if you expect them to be displayed at the same time on a post.
Issue Description
Steps to Reproduce:
foo
and a subfield namedbar
with the field keyfield_12345
.Expected Behavior:
update_field('foo_bar', $value, $postId, $field_group_key)
, the function should accept a extra parameter to update the field in the appropriate group.update_field('field_12345', $value, $postId)
, the function should correctly identify and update the right meta key.Actual Behavior:
update_field('foo_bar', $value, $postId)
updates the first field group it finds.update_field('field_12345', $value, $postId)
does not work as expected. It triggerswp_update_meta('post', $post_id, 'bar', $value)
instead of the correct meta key, causing an update to the wrong meta key.Detailed Description:
When creating two field groups with identical names and fields, but different display conditions based on taxonomies, the
update_field
function exhibits unexpected behavior. Specifically, using the field key to update a field results in an incorrect meta key update. This issue seems to stem from the wayupdate_field
resolves field keys and meta keys.Also, there is a lack to specify the right fields group in this circumsistence.
Steps to Reproduce Code Example
Create two field groups:
foo
with a subfieldbar
(key:field_12345
) assigned to Taxonomy A.foo
with a subfieldbar
(key:field_12345
) assigned to Taxonomy B.Use the following code to update the field values:
Observed Issue
update_field('foo_bar', $value, $postId)
updates the first matching group correctly.update_field('field_12345', $value, $postId)
incorrectly triggerswp_update_meta('post', $post_id, 'bar', $value)
, leading to an update with the wrong meta key.Suggested Solution
Review and refine the logic within
update_field
to ensure it correctly resolves field keys to the appropriate meta keys, considering the field group option.Environment
Thank you for your attention to this issue. Looking forward to your response and potential resolution.