I'm using ACF's update_sub_field function to add content from a loop to a child repeater field.
I've got it to a point where the data is being added to the child repeater but it's overwriting itself. It only ever updates the first row in the child repeater so I only ever see the last loop through.
I'm iterating the parent repeater using $counter
, but when I try to add an iteration to the child repeater it breaks the function. Like so:
update_sub_field( array($field_key, $counter, $sub_field_key, $rowcount), $value, $post_id );
I've tried another function add_sub_row
... this adds the correct number of rows to the child repeater's but doesn't add the data.
Here's my full code:
// Product Code Titles
$rows = $html->find('div[class=product-table]', 0)->find('tr');
$field_key = "field_5ae0882f9d6f9";
$sub_field_key = "field_5ae088999d6fb";
if(empty($rows)){
die("Empty array");
}
$titles = array(); // aka your $data
// here we fetch the first row and iterate to get titles
$row = current($rows);
foreach ($row->find('td') as $cell) {
$titles[] = array("column_title" => strip_tags($cell->innertext));
}
update_field( $field_key, $titles, $post_id );
// here we continue iteration starting from second row
$value = array();
$rowcount = 1;
while($row = next($rows)){
$cells = $row->find('td');
$columnsCount = count($cells);
$counter = 1;
foreach ($cells as $cell) {
$value[] = array("text" => strip_tags($cell->innertext));
update_sub_field( array($field_key, $counter, $sub_field_key), $value, $post_id );
echo '<pre>'; print_r($value); echo '</pre>';
$value = array();
$counter++;
}
$rowcount++;
}
Just to give some context, I'm recreating this table with the cell data being put into a child repeater field.