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


Resolving lost data when moving field to group?

  • When moving existing fields (with data added) to new group fields, data is lost due to the way groups save data for child fields with the parent group name, e.g. ‘parent_child’.

    Is there a way of re-mapping data so that existing ACF fields can be reorganised in this way?

  • I would be keen to get an answer to this as well.

  • 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.

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

You must be logged in to reply to this topic.