Home Forums Add-ons Repeater Field update a acf sub_field with the value from a different sub_field


update a acf sub_field with the value from a different sub_field

  • I have a ACF repeater field ‘add_full_address’ for an address with 4 sub_fields.
    The user fills in their address in their profile.
    I would like this field to also show on the a different post type ‘general_info’
    I’ve created another ACF repeater field ‘c_address’ with 4 sub_fields connected to the 2nd post type ‘general_info’.
    I need write some code so the sub_fields of ‘c_address’ are updated with the data from the ‘add_full_address’ fields.
    I’ve tried

    function my_load_field($field) {
    $post_id = get_the_id();
    $post_author_id = get_post_field( ‘post_author’, $post_id );
    $field[‘readonly’] = 1;
    if( have_rows(‘add_full_address’, ‘user_’ . get_post_field( ‘post_author’, $post_id )) ):
    while( have_rows(‘add_full_address’, ‘user_’ . get_post_field( ‘post_author’, $post_id )) ) : the_row();
    $value = get_sub_field(‘c_address_1’);
    $field[‘value’] = $value;
    return $field;
    add_filter(“acf/load_field/key=field_573d88ef3f3bb”, “my_load_field”);
    but it isn’t working for a repeater field. (only plain text)

    I also tried to use update_sub_field() with a test value ’23’ I put it in the function my_load_field2($field) but it also doesn’t work.
    How should I do this?

    function my_load_field2($field){
    $post_id = get_the_id();
    $post_author_id = get_post_field( ‘post_author’, $post_id );
    $field[‘readonly’] = 1;
    $field_key = “field_573d88ef3f3bb”; //key of the sub_field
    $user_id = ‘user_’ . get_post_field( ‘post_author’, $post_id);
    $value = get_field($field_key, $user_id);
    $value[] = “23”;
    $field = update_field( $field_key, $value, $user_id );
    return $field;
    add_filter(“acf/load_field/key=field_573d88ef3f3bb”, “my_load_field2”);

  • Hi @renar

    You need to do it by using the acf/save_post hook instead. That way, the data will be updated when your users are saving the address.

    Also please use the update_field() function with the field key of the repeater field instead of the subfield. This page should give you more idea about it:

    I hope this helps. If you have any other questions, feel free to ask me again 🙂

  • I tried:

    function my_acf_post_fieldb($post){
    $post_id = get_the_id();
    $post_author_id = get_post_field( ‘post_author’, $post_id );
    $field[‘readonly’] = 1;
    $field_key = “field_573d8df4646da”;// field_key of repeater field
    $value = array(
    “c_address1” => “Foo”,
    “c_address2” => “Bar”
    $field = update_field($field_key, $value);
    return $field;
    add_filter(‘acf/save_post hook’, ‘my_acf_post_fieldb’);

    and it isn’t working either. The fields are not being populated with values.
    What do I need to return out of the function? I tried return $post; it doesn’t work either.

  • Hi @renar

    I’m afraid you have several mistakes there. The hook should be “acf/save_post”, not “acf/save_post hook”. Here’s an example how I do it:

    function my_acf_post_fieldb($post_id){
        // Field key where you want to update (c_address)
        $field_key = 'field_573d8df4646da';
        // The General Info post ID that you want to update
        $general_info_post_id = 99;
        // Get the saved value from the user profile
        $value = get_field('add_full_address', $post_id);
        // Check if the value exists or not
        if( $value ) {
            // Update the repeater field in the selected post
            update_field($field_key, $value, $general_info_post_id);
    add_filter('acf/save_post', 'my_acf_post_fieldb', 20);

    I’m not sure how did you set the custom field to the General Info post type, so I just assume that you want to update the value on a selected post.

    I hope this makes sense 🙂

  • Your code works, but only for a text field, not for a repeater field.
    I got it working for a repeater field using the code below:
    `function my_acf_post_fields($post_id){
    $post_id = get_the_id();
    $post_author_id = get_post_field( ‘post_author’, $post_id );
    if( have_rows(‘add_full_address’, ‘user_’ . get_post_field( ‘post_author’, $post_id )) ):
    $i = 0;
    while( have_rows(‘add_full_address’, ‘user_’ . get_post_field( ‘post_author’, $post_id )) ) : the_row();
    $value{$i} = get_sub_field(‘add_address_1’);
    $value2{$i} = get_sub_field(‘add_address_2’);
    $value3{$i} = get_sub_field(‘add_city’);
    $value4{$i} = get_sub_field(‘add_zip_code’);

    if( have_rows(‘c_address_of_your_clinics’) ) {
    $i = 0;
    while( have_rows(‘c_address_of_your_clinics’) ) {
    $i++; update_sub_field(‘c_address1’, $value{$i});
    update_sub_field(‘c_address2’, $value2{$i});
    update_sub_field(‘c_city’, $value3{$i});
    update_sub_field(‘c_zipcode’, $value4{$i});
    add_filter(‘acf/save_post’, ‘my_acf_post_fields’, 20);

    However, it is not lopping through all the rows, it stops at the first row.
    How do I make it look through and display all the row values.
    Do I need to change the order of the if() and while() loops?

  • Hi @renar

    The loop stopped because you executed the the_row() function in the existing loop. Also, please don’t use get_the_id() in functions.php as it can get any ID depends on how it’s executed.

    If my code works for a text field, it should be working for a repeater field too. Could you please share the JSON or XML export of your field groups so I can test it out on my end?

    Thanks 🙂

  • This reply has been marked as private.
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘update a acf sub_field with the value from a different sub_field’ is closed to new replies.