Support

Account

Home Forums Add-ons Repeater Field Can you update a row in a repeater?

Solved

Can you update a row in a repeater?

  • Just as the title says – I have a repeater row filled with dates associated with some other post objects. I need to be able to update those dates from the front end. Normally I’d use this:

    $field_key = "repeater_field";
    $post_id = "event_123";
    $value = get_field($field_key, $post_id);
    $value[] = array("sub_field_1" => "Foo", "sub_field_2" => "Bar");
    update_field( $field_key, $value, $post_id );

    You know, like in the documentation… but that just ADDS a row, right? It’s not going to update an existing row. So is there a way to update a row that already exists without going through the admin?

  • Hi @twistysnacks

    $value holds an array of rows. It is this array that you save.

    All you need to do is loop through the array and modify the row you want, then save the array like normal.

    It is the line

    
    $value[] = array("sub_field_1" => "Foo", "sub_field_2" => "Bar");
    

    That ‘adds’ to the array, by removing this, there will be no rows added

  • Awesome, I will give that a try. Thank you.

  • seriously.. marked your own thanks as the solution? =P

  • You’re right. How embarrassing. I couldn’t possibly expect people viewing this forum to be smart enough to recognize that my thanks isn’t the solution. Let me get right on that for you, sir.

  • I think what Jonathan was getting at is that it’s not helpful for other users looking for the solution. To your point, it wouldn’t be difficult to find the solution in this case, but it could be for threads with multiple replies; so in principle, its probably not a good practice.

  • It’s not something I can edit now, and I did so because it told me that “Only the topic author can mark an answer” and it was giving me the option as I was making a new post. So. Shrug. This is strange that we’re having this conversation.

  • I’m having the same issue…sorta. I have the following field defined:

    Repeater Field

    Through code, I am filling out the values of the repeater like so:

    	$emergency_contact = array('emergency_contact_full_name' => $EMERGENCY_CONTACT_FULL_NAME,
    								'emergency_contact_relationship_to_you' => $EMERGENCY_CONTACT_RELATIONSHIP,
    								'emergency_contact_phone' => $EMERGENCY_CONTACT_CONTACT_PHONE
    
    						);
    	update_field('volunteer_emergency_contact', $emergency_contact, $theId);

    No errors when that code runs.

    But when I try to load the CPT entry, WordPress just hangs and nothing happens. If I remove that line where the code gets updated, everything works just fine.

    Am I supposed to create something to update the row in the repeater beforehand? Am a bit confused.

    Thanks in advance!

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

The topic ‘Can you update a row in a repeater?’ is closed to new replies.