Support

Account

Home Forums Add-ons Repeater Field How to add new row at 0 index in repeater field?

Solved

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();
    }

    Rent Repeater Field

    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.

  • How can i get values from previous posts?

  • 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

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How to add new row at 0 index in repeater field?’ is closed to new replies.