Home Forums General Issues Resolving lost data when moving field to group? Reply To: Resolving lost data when moving field to group?

  • The simple answer to your question is that this is extremely difficult to do without making many changes to the database. You have to find all of the fields in the DB and alter the meta key value to match the new name used by ACF.

    What you would need to do is to do a query on to get all posts, then loop over the post, get the old field value using get_post_meta() (because ACF will not be able to find this field using get_field()) then use update_field() to update the value of the new group field. If you have many posts this is likely to fail do to timing out. It can be done directly in the database but this would be prone to errors if the old field name is not unique.

    When I make interface changes of this nature I do the following
    (this is a quick explanation as there is a lot of code that would be involved)

    1. Create new field(s) – group and sub fields
    2. add an acf/prepare_field filter for the new sub field. If the new field does not have value (NULL) then populate the value with the old field value
    3. add an acf/save_post action that deletes the old value when a post us updated using the new field(s)
    4. add an acf/prepare_field filter to the old field that returns false to hide the old field but keep it definition intact
    5. In the template code where the field is used, get the new field value, if the value is NULL (never updated) then get the value from the old field

    This allows you to alter the field setup without needing to alter all of the existing data and will transition the fields from old to new as a post is updated. If a post is never updated then it will continue to work indefinitely.