I have two pages that are location for the same flexible content group. The group consists of a flexible content field that contains a repeater which contains another flexible content. And I want to copy a row from this last nested flexible content, to the corresponding flexible content in the other page.
This is the code I have so far, but is not working.
With this function I get the fields structure:
function kb_update_elements_target($post_id, $flexibleContent) {
$rows = array();
if (have_rows($flexibleContent, $post_id)) {
while (have_rows($flexibleContent, $post_id)) {
$rows[] = the_row();
if ( get_row_layout() == 'layout_hero' ) {
if (have_rows('hero_row', $post_id)) {
while ( have_rows( 'hero_row', $post_id ) ) {
the_row();
if ( have_rows( 'hero_elements', $post_id ) ) {
while ( have_rows( 'hero_elements', $post_id ) ) {
the_row();
if ( get_row_layout() == 'heading_element' ) {
get_sub_field( 'heading', $post_id );
}
}
}
}
}
}
}
}
return $rows;
}
Then another function that is called through ajax get this data and updates the target page flexible content field:
$templateSection = kb_update_elements_target($sectionTemplateID, 'flexible_sections');
$pageContent = kb_update_elements_target($currentPageID, 'flexible_sections');
array_splice( $pageContent, 0, 0, $templateSection );
update_field( 'flexible_sections', $pageContent, $currentPageID );
But is not working, I can’t add the nested flexible content row in the Template into the corresponding field on the target page. Could someone point me in the right direction?
Thank you.
Jorge