What format to use when updating ACF Repeater-subfields in Wordpress with Ajax POST

34 Views Asked by At

When sending an Ajax-post request it yields 400 Bad Request because of the ACF-formatting, I guess. See code example below. There is no problem when updating an ACF text-field or other simple type.

The ACF field-names are all correct.

How should the acf-repeater formatting look like?

updateModel(e) {
    var thisModel = $(e.target).closest("li");
    var ourUpdatedPost = {
      title: "test",
      acf: {
        // repeater acf field name
        model_price_size_and_image: [
          {
            // subfield-names + value
            model_repeater_code: "123",
            model_repeater_price: "400",
            model_repeater_image: "1212",
          },
          {
            model_repeater_code: "321",
            model_repeater_price: "600",
            model_repeater_image: "1213",
          },
        ],
      },
    };
    $.ajax({
      beforeSend: (xhr) => {
        xhr.setRequestHeader("X-WP-Nonce", myData.nonce);
      },
      url:
        myData.root_url +
        "/wp-json/wp/v2/model/" +
        thisModel.data("id"),
      type: "POST",
      data: ourUpdatedPost,
      error: (response) => {
        console.log("Sorry");
        console.log(response);
      },
      success: (response) => {
        this.makeModelReadOnly(thisModel);
        console.log("Congrats update");
        console.log(response);
      },
    });
  }
}
0

There are 0 best solutions below