Support

Account

Home Forums Backend Issues (wp-admin) Trouble updating repeater field values from a custom plugin

Solved

Trouble updating repeater field values from a custom plugin

  • Hi there,

    I’m trying to update a repeater field value using update_sub_field() in a custom plugin. I create the post and then updated the fields. It works great with normal fields e.g. update_field, however i’m having trouble adding rows to a repeater.

    My repeater is called video_urls and has a text field called url.

    I’m using populating it as part of a loop:

    foreach ($repeaterFields[$name] as $row) {
        update_sub_field(array($name, 1, $row), $column, $productId);
    }

    Which looks like:
    ["video_urls",1,"url"], https://www.my-video-url.com, 414

    414 is a valid post that I can view, but the URL’s not being updated. What am I missing here, i’ve followed the documentation:
    update_sub_field( array('repeater', 1, 'caption'), 'This caption is for the first row of the repeater!' );

    Thanks

  • If you know all of the values then it is easier to update the repeater rather than each sub field.

    You may also be running into an issue with using field names VS field keys. When updating ACF fields when they do not already exist in the DB you need to use the field key.

    To update a repeater in one go.

    
    // making up field keys
    // you need to get the correct ones for your fields
    
    // construct an array for the repeater
    $value = array(
      // one nested array for each row
      array(
        // field key => value pairs
        'field_123456' => 'value',
        'field_234567' => 'value',
        // etc
      ),
    );
    
    update_field('field_987654', $value, $post_id);
    
  • That worked a treat! Thanks!

    For anyone else viewing this, you can find the field key by inspecting the input element on your post.

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

You must be logged in to reply to this topic.