---
title: 'Advanced Custom Fields - update_sub_field() for repeater fields'
url: 'https://www.nick-hayward.co.uk/advanced-custom-fields-update_sub_field-for-repeater-fields'
markdown: 'https://www.nick-hayward.co.uk/advanced-custom-fields-update_sub_field-for-repeater-fields.md'
date: '2022-10-20'
description: "I've been working on a WordPress web design project using a hand made front-end form to update an Advanced Custom Fields repeater field via AJAX. This was a bit of a stretch for me as I've never used AJAX before and I hit a wall when 99% there!"
taxonomy:
  category:
    - 'Web Development'
    - WordPress
---

[Home](https://www.nick-hayward.co.uk/) » Advanced Custom Fields - update\_sub\_field() for repeater fields

 ![update_sub_field-acf-repeater-wordpress.png](https://www.nick-hayward.co.uk/images/9/d/d/5/6/9dd56cfbd7ee73ba9578984b8768742b5fe1458f-updatesubfield-acf-repeater-wordpress.png) 

 October 20th, 2022 Posted in [Web Development](https://www.nick-hayward.co.uk/taxonomy?name=category&value=Web%20Development), [WordPress](https://www.nick-hayward.co.uk/taxonomy?name=category&value=WordPress)

# Advanced Custom Fields - update\_sub\_field() for repeater fields

I’ve been working on a WordPress web design project using a hand made front-end form to update an [Advanced Custom Fields](https://www.advancedcustomfields.com/) repeater field via AJAX. This was a bit of a stretch for me as I’ve never used AJAX before and I hit a wall when 99% there!

I hit a snag with the **update\_sub\_field()** function to target the right repeater row and field to update. There were two issues – one is that the rows don’t start from 0 like I expected and there’s a functions.php snippet to fix that ([bottom of this page](https://www.advancedcustomfields.com/resources/update_sub_field/)).

The second was that the [documentation](https://www.advancedcustomfields.com/resources/update_sub_field/) was a bit ambiguous in describing how the data needs to be sent to the back end (for someone unfamiliar with arrays, like me), with this example being given:

```
// target a nested sub field directly.
update_sub_field( array('repeater', 1, 'sub_repeater', 2, 'sub_sub_field'), 'This value is for repeater row 1, and sub_repeater row 2!' );
```

I replicated this with my incoming data from the AJAX function but it wouldn’t update unless I hard coded it in the **update\_sub\_field()** PHP function. I thought the data was exactly the same, but it actually wasn’t – it had quote marks around it so was being treated like a string rather than converted to an array.

ACF support suggested making sure the array was sent back correctly and a developer friend fixed this quickly by exploding the query string into an array before passing it to **update\_sub\_field()**

Part of my PHP function that the AJAX call sends data back to:

```
$post_id = $_POST['post-id'];
$memberName = $_POST['member-name'];
$acfPath = $_POST['acf-path'];
// Convert the query string to array - thanks Ewan!
$combined = explode(",", $acfPath);
// Now sending $combined instead of the raw query string in $acfPath
update_sub_field($combined, $memberName, $post_id);
```

So that fixed things and I hope this quick blog helps anyone else struggling with **update\_sub\_field()** updating the Repeater field in Advanced Custom Fields

---

## Navigation

- Parent: [Home](https://www.nick-hayward.co.uk/index.md)
- Previous: [Ecommerce Enlightenment ebook – moving from Amazon to Gumroad](https://www.nick-hayward.co.uk/ecommerce-enlightenment-ebook-moving-from-amazon-to-gumroad.md)
- Next: [3 unusual WordPress web design projects](https://www.nick-hayward.co.uk/3-unusual-wordpress-web-design-projects.md)

<!-- Added by Intention Header Footer Code plugin -->
<!-- Privacy-friendly analytics by Plausible -->
<script async src="https://plausible.io/js/pa-dVyPIkxnhGVxiRfGP1Cj2.js"></script>
<script>
  window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
  plausible.init()
</script>


<!-- Added by Intention Header Footer Code plugin -->
<style>
h1, h2,h3,h4 {
  text-wrap: pretty;
}
</style>
