AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
823 stars 168 forks source link

update_field() not working as expected in this scenario #913

Closed alexsoluweb closed 1 month ago

alexsoluweb commented 1 month ago

Issue Description

Steps to Reproduce:

  1. Create two identical field groups with the same names and fields embedded in groups.
  2. Assign each field group to a different taxonomy within a post type.
  3. For example, let's have a group named foo and a subfield named bar with the field key field_12345.

Expected Behavior:

Actual Behavior:

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 way update_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

  1. Create two field groups:

    • Group 1: foo with a subfield bar (key: field_12345) assigned to Taxonomy A.
    • Group 2: foo with a subfield bar (key: field_12345) assigned to Taxonomy B.
  2. Use the following code to update the field values:

    
    // This should have an option to specify which field_group we want to save
    update_field('foo_bar', $value, $postId);
    
    // This should update the field with the field key
    update_field('field_12345', $value, $postId);

Observed Issue

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.

lgladdy commented 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

alexsoluweb commented 1 month ago

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.

lgladdy commented 1 month ago

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.