Support

Account

Home Forums Add-ons Repeater Field Multiple fields -> to repeater field with subs

Solved

Multiple fields -> to repeater field with subs

  • Hello,

    I had 5 specific separate fields filled with data in posts. Now I have a need to turn those 5 fields into one repeater field. I.e. I have fields “name”, “number”, “address” and now I want to have a repeater called “person” with subs “name”, “number”, “address”. Seems simple.

    I noticed that I can create a repeater field and then drag-n-drop existing fields into the repeater. And it seems to work just fine, my old data should not be deleted and I should achieve what I need.

    But the problem I am facing is that after these changes on front end I can see my data show (still using get_field – will change that), but on back-end (editing post) my data is not showing. So I see an empty repeater field with an option to add new rows. But old data is not visible.

    Am I missing something? I though that plugin will automatically update the fields to make them “repeater sub-fields”. Isn’t that the case? What could I do to make that data appear in back-end?

  • When you move a field into a repeater you are effectively changing the field name. The old data is in the DB under the old name, for example "name", and you can continue to get the value using get_field('name') however, this is no longer the real name of this field and you cannot get values added in the repeater in the same way. Your new repeater field needs to use a have_rows() loop and use get_sub_field('name') to get the values https://www.advancedcustomfields.com/resources/have_rows/

    ACF does not move the content stored in existing fields when the field names are changed, or moved into or out of repeaters, which is basically the same thing. The reorganization changes the field names.

    You would need to build code to do this moving and you would also need to build code to check it.

    For example, in your template where you are showing these values you would need to do something like

    
    if (have_rows('repeater')) {
      // values have been added to the repeater
    } else {
      // nothing in repeater
      // look in the old field names
    }
    

    In the admin you can use an acf/load_field filter on the repeater https://www.advancedcustomfields.com/resources/acf-load_field/. In this filter you can do the same checking. If the value of the field is empty then get the values from the old field and dynamically populate the first row of the repeater with the old values. You could also create an acf/save_post action https://www.advancedcustomfields.com/resources/acf-save_post/ that would delete the old values using delete_post_meta().

  • So basically I could just do a one-time loop over my posts to see if they have field “name” filled and if so, update sub_field (rather add_row) with the data? That way I would transfer my data to repeater.

  • Hi again. So I wrote this very basic code for myself to transfer my data from old custom fields to repeater fields:

        foreach ($myposts as $mypost) {
    
            $name = get_field('name', $mypost);
            $date = get_field('date', $mypost);
    
            if($name) {
            	$row = array(
            		'name' => $name,
            		'date' => $date,
            	);
    
            	$i = add_row('nameAndDate', $row, $mypost);
            }
    

    It transfers the data and it shows up in get_sub_field(). But not in admin panel (post edit) where I should be able to see that data in repeater field section. Whats the problem?

  • Still waiting for help. Anyone?

  • Ok, so apparently the problem was that I created repeater fields with the same names as there were normal fields. And system did not like that at all. O tried my code with newly created repeater fields and it works.

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

The topic ‘Multiple fields -> to repeater field with subs’ is closed to new replies.