Support

Account

Home Forums Add-ons Repeater Field Convert Field to Repeater without Loosing Existing Data

Solving

Convert Field to Repeater without Loosing Existing Data

  • Hi there! I feel like this topic must have been asked/covered before, but I searched all over and couldn’t find any existing topics.

    I am using ACF so a client can upload a file to a custom post type. We already have about 120 entries/files uploaded. What I would like to do is convert the file field to a repeater so that multiple files can be uploaded without loosing the existing files. Is there any way to do this? Thanks!

  • I think you’d have to do in manually by creating a script to do the work.

    You field, not as a repeater requires two postmeta entries

    • your_field_name => your field value
    • _your_field_name => ACF field id: this field name is the same as your field name prefixed with an underscore and contains a reference to the ACF field id generated when creating the field.

    When you have a repeater this changes to

    • repeater_field => count of rows
    • _repeater_field => ACF field id of the repeater
    • repeater_field_X_sub_field => each row subfield value, the X is the index of the row starting at 0
    • _repeater_field_X_subfield => ACF field id of the sub-field

    My suggestion would be to leave it as it is and create a new field for your repeater. Tell the client that on new posts to ignore the old field and use the new one, and when updating an old post to move the old images to the new field. Then in the front end use values from both fields in the display.

  • Similar to Robby, I’ve been faced with this a couple of times. What I would like to know is: Would it be possible, with ACF Pro, to have it so that if you drag an existing field into a repeater, ACF would update the corresponding saved data automatically on update?

  • I came across the same problem today (noticed i had repeaters with 1000s of rows) and stumbled upon this thread.

    I created the following script to convert the data of a file field into a repeater. This worked when there was a file uploaded (field has data) and should be inserted into array of repeater on first position.

    https://gist.github.com/slackday/947569a2adf7300e26027a920a20ede0

    Use at your own risk! I’d advise to make database backup and test in local environment first.

  • To answer the question by @squarestarmedia

    No, this is not possible. When you are editing a field group ACF does not know what data in the database is saved using the field. In addition to this, when the group is saved, ACF does not know that the field use to be a top level field and is now nested in a repeater (or visa-versa). Nor does in know if you’ve changed to location rules of the group.

    The data stored for a field is related to a post or some other object.

    The field group is not related to anything. The fields in the group are related to the group.

    In order to associate the field to the data when getting or saving a post (or other object) ACF needs 2 important pieces of information the first is the post ID and the second is the field key. ACF has the field key when saving a field group, but it does not have the post IDS for every post the a value is saved to.

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

The topic ‘Convert Field to Repeater without Loosing Existing Data’ is closed to new replies.