Hi All
My website has used repeater fields to generate dynamic layouts but it is harder and harder to navigate the more sections you create, plus you need a row for every different field “type”.
When I first created the site repeater seemed like a good idea but now I want to move the field over to a flexible content field so I have more control and can add functionality in more easily.
How difficult would it be to move them over without causing to much of a headache? Looking at the syntax they look pretty similar and I am hoping it will just be a little bit of rearranging rather than a full rewrite.
Anyone else done this and can tell me?
Hi @kyon147
If you have the right code, I believe it won’t be hard. You can loop through the posts and use the update_field() function to update the new flexible content like this:
$post_id = 99;
$flex_field_key = "field_123456789";
// Get the repeater values
$repeater = get_field('repeater');
// Create an array to store the new flexible values
$flexible = array();
// Loop through the repeater
foreach( $repeater as $row ){
// Set the values
$flexible[] = array(
"flex_sub_field_1" => $row['repeater_sub_field_1'],
"flex_sub_field_2" => $row['repeater_sub_field_2'],
"acf_fc_layout" => "layout_1_name"
);
}
// Update the new flexible field
update_field( $flex_field_key, $flexible, $post_id );
After the repeater field is copied correctly, you can delete it.
Please don’t forget to backup your site and database first in case something bad happens.
I hope this helps 🙂