Home › Forums › Add-ons › Repeater Field › How to add new row at 0 index in repeater field?
I’ve Rent Repater Field with 9 sub fields. I am creating a new row with add_row(). By using add_row() function it adds a new row at last index. But client want to add new row on 0 index.
function CreateNewRentRepeaterField()
{
$id = $_POST['id'];
//Create an empty rent repeater field for selected ids
foreach ($id as $key) {
$rent_repeater = array(
'rent_collected' => '',
'paid_date' => '',
'security_deposit' => '',
'added_to_wave_income' => '',
'repairs_purchases' => '',
'paid_to_owner' => '',
'added_to_wave_payments' => '',
'utilities_paid' => '',
'commission' => ''
);
add_row('rent', $rent_repeater, $key);
}
die();
}
Can anyone tell me how can i add a new row at 0 index?

There is no 0 index, repeaters start at 1 which is in the doc https://www.advancedcustomfields.com/resources/add_row/, when using. You would need to add a new row add_row() which will be added at the end and then update each row update_row() https://www.advancedcustomfields.com/resources/update_row/ with the value from the previous row and finally update the first row with the new value.

giving it some thought, no, you don’t need to loop at all, or use add_row() or update_row()
Quick example, not precise or including everything.
// this gets the repeater an all rows in an array
$repeater = get_field('repeater_field');
// create a new row, should be same format as returned by above
$new_row = array();
// push the new row onto the beginning of the repeater
array_unshift($repeater, $new_row);
// you can use update field to update the entire repeater at once
update_field('repeater_field', $repeater);
hey John, Thanks a lot man. You saved a lot of my time 🙂 really appreciated your help. Thanks again
The topic ‘How to add new row at 0 index in repeater field?’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.