Support

Account

Home Forums Add-ons Repeater Field Programmatically updating repeater fields

Solving

Programmatically updating repeater fields

    • darylb

    • April 24, 2021 at 7:34 am

    Hello! We are currently experiencing a problem trying to update a repeater field dynamically via PHP.

    The repeater is inside a group called “Community Resources Information” – so the structure is as follows:

    Community Resources Information (group) > phone_numbers (repeater) > two text fields: phone_number and phone_description.

    We are using field keys to identify both the group, repeater, and text fields.

    Here’s a code snippet of what we’re using to attempt to populate the repeater:

    $post_id = $post->ID;
    // this is the group key for the Community Resources Information group
    $group_key = 'group_6050d4528b96a';
    $value = array(
        // this is the field key for the repeater
        'field_6050d513a3996' => array(
            array(
            // these are the field keys for the phone_number and phone_description text fields
    	    'field_6050d550a3997' => '(123)456-7890',
    	    'field_6050d596a3998' => 'phone number description'
    	)
        ),
    );
    
    update_field($group_key, $value, $post_id);

    By comparison, here are 3 regular text fields that we are using which are updating successfully:

    update_field('description', 'description goes here', $post_id);
    update_field('email_address', 'email address goes here', $post_id);
    update_field('website_url', 'website url goes here', $post_id);

    Additional context:
    – We have attempted to use add_row and update_row, but both were also unsuccessful.
    – We are running this from within a custom plugin that we are building.
    – The fields that we are trying to update are empty; no other data is present.
    – If I manually enter a data row into the repeater (via wp-admin), and then run the plugin, the data that I entered is removed completely, but no new data shows up in its place.

    Has anybody ever run into this type of situation before? Is there anything with our structure that could be hindering the population of data? Are we using the correct methodology in order to populate data, or should we be approaching this in a different way?

    Any help with this would be hugely appreciated! Thanks very much!!

  • You do not need the field group key when updating a repeater

    
    $repeater_key = 'field_6050d513a3996';
    $value =  array(
      array(
        'field_6050d550a3997' => '(123)456-7890',
        'field_6050d596a3998' => 'phone number description'
      )
    );
    
    update_field($repeater_key, $value, $post_id);
    
    • darylb

    • April 24, 2021 at 10:01 am

    Thanks so much for the quick response John! I’ve tried it with your code snippet, updating the repeater directly, but unfortunately the repeater still does not update. I’ve double-checked the field keys to ensure they are correct, and re-confirmed that other Text and Number fields update without issue.

    I’ve attached a screenshot of how we have the repeater configured in ACF, just in case there’s something there that might be stopping this from working.

    Phone Numbers repeater setup in ACF

    Thanks again so much for your help!

  • You have me stumped. I’ve looked over this a couple of times and I’ve even did a test of my own it’s working as expected. I can only assume that there is a conflict somewhere. Do you have any acf/update_value filters that might be acting on these fields?

    • darylb

    • April 24, 2021 at 11:37 am

    That’s a possibility – we’ll start investigating that. Thanks for the lead, I will keep you posted! And thanks again for your quick response!

    • darylb

    • April 24, 2021 at 11:44 pm

    Some progress to report – I tried moving your example completely out of our plugin and dropped it into a WordPress template, and that successfully updated the repeater!

    So, we now know it’s something about calling that code from within the plugin that is causing repeater updates not to function (but allowing Text and Number fields to be updated without issue, for some reason).

    I tried identifying the post ID manually from within the plugin, like so:

    $repeater_key = 'field_6050d513a3996';
    $value =  array(
    	array(
    		'field_6050d550a3997' => '(123)456-7890',
    		'field_6050d596a3998' => 'description sent from plugin'
    	)
    );
    
    update_field($repeater_key, $value, 48058);

    Sadly, this did not work either – but we’re making progress!

    So the question now becomes: Is there anything that could possibly block repeater (or perhaps just nested?) updates from happening when done within a plugin?

  • It could be when you are calling update field. If you are trying to update the field before the acf/init hook fires naturally calling an acf function could cause a premature initialization of ACF and this can cause strange things to happen. The acf/init hook fires on the WP init hook with a priority of 10.

    • darylb

    • April 25, 2021 at 4:44 am

    We’ve also tried your code snippet (outside of any function) in functions.php, and it works just fine there as well.

    We’re trying to now call the update_field functionality from functions.php itself by putting it within its own function, that we could then call outside of it. Something like this would go in functions.php:

    function update_repeater_field($field_to_update, $value, $post) {
      update_field($field_to_update, $value, $post);
      echo "function update_repeater_field triggered.<br/><br/>";
    }
    add_action('acf/init', 'update_repeater_field', 10);

    And then we would call update_repeater_field($field_to_update, $value, $post); from our plugin. No success yet, the plugin sees the function as undefined, but theoretically that functionality should work, correct?

    Thanks again for your continued help!

  • You are calling the function from the plugin before the theme is initialized, which means that you are calling it before the after_theme_setup hook, this also means that you are attempting to call the acf function before the init hook which means that you are trying to call acf function before acf is initialized.

    https://codex.wordpress.org/Plugin_API/Action_Reference

    • darylb

    • April 27, 2021 at 6:45 am

    Victory!

    Your comment on when we were calling update_field turned out to be right on the money. Interestingly, it was initialized enough to be able to run update_field on regular Text/Number fields, but not enough to allow Repeater fields to be updated.

    So, we added an add_action which ensured that our plugin fired after all others had, like so:

    add_action('plugins_loaded', 'name_of_function');

    This solved the issue! Thank you SO much for all of your help and responsiveness, it is VERY much appreciated! We owe you big time!

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.