Home › Forums › Add-ons › Repeater Field › Add_row to nested repeater field (child only) › Reply To: Add_row to nested repeater field (child only)
HI WooHoo,
Yes. I run function add_row in my repeater_field loop.
In ma case I have:
Lesson / main repeater
– Lesson active / subfield true/false
– Lesson finished / subfield true/false
– Notes / nested repeater
— Note – custom sub field
— Note date – another custom sub field
I need to create empty Notes row at first page run for each lesson. This is a part of much complicated script. It`s a little complicated to explain 🙂
if( have_rows('lesson') ):
while ( have_rows('lesson') ) : the_row();
//////////////////////////////////////////////////////
// Generate empty rows for each lesson at first run
//////////////////////////////////////////////////////
// here i have date field which is empty at first start
// date is generated at the end
if (get_field('start_date') == ''){
$row = array(
'lesson_date' => 1,
'lesson_finished' => 0,
);
// add_row('lesson', $row); // this wont work
add_row('field_579eeb638c835', $row); // you must take key of custom field "lesson"
}
endwhile;
endif;
// fill the field with current date
if (empty(get_field('start_date'))){
update_field('start_date', date(Ymd));
}
Then I need to add notes to lesson no2 – I have for exaple 5 lessons – 5 main repeater field row.
//This is a part of ajax call
$lesson_id = 2; // main repeater field
if( have_rows('lesson',$post_id) ):
while ( have_rows('lesson',$post_id) ) : the_row();
if( get_sub_field('notes', $post_id) ){
$notes_row = get_sub_field('notes', $post_id);
} else {
$notes_row = array();
}
// Check variables for fallbacks
if (!isset($input_text) || $input_text == "") { break; }
array_push($notes_row, array( 'note' => $input_text , 'note_date' => date('d.m.Y') ));
$notes_row = array( 'notes' => $notes_row );
update_row('lesson', $lesson_id , $notes_row, $post_id);
}
endwhile;
endif;
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.