Support

Account

Home Forums Add-ons Repeater Field Repeater Field not playing well using update_field()

Solved

Repeater Field not playing well using update_field()

  • I think there is a problem with the update_field() function with a repeater field. Firstly, everything is up to date, so that’s good to go.

    I’m trying to add in repeater data on the front end, I create an array with the appropriate field names and it seems to work fine. When I go to edit the post in the admin with the updated repeater data I get various errors, memory allocation or timeouts, I’ve changed my php.ini values, so that’s not the case.

    Doing some digging, I think the issue is how the data is stored in the DB. When I add the data from the update_field() function, the post meta for that field is stored as a serialized array, but when I update it manually from the edit post page, the DB information is way different, creating meta keys for each field in the repeater like this: _repeater_field_name_0_field_name & repeater_field_name_0_field_name. The actual field for the repeater is a simple “1”.

    This is kinda weird as I thought running the update_field() function with a repeater field would automatically configure this information for the DB.

    Please let me know how to solve this. Thanks.

  • Hi @gregattack

    Are you using the update_field on a $post which already has a value? Or are you saving this repeater value to a new $post?

    If you are saving to a new post, then ACF will not know that it is a repeater field! To bypass this issue, please first read the update_field docs and pay attention to the $field_key parameter.

    If you use the field_key, instead of the field_name, the repeater field will be detected and the data will be saved correctly.

    Hope that helps.

    Thanks
    E

  • It is a new post and I am using the field_key:

    update_field(“field_52cb11f540cd9”, $variable, $post_id);

    with an array with the correct repeater field names and it’s still not inputting it correctly into the DB.

  • Hi @gregattack

    Can you please debug the update_field function found in core/api.php on line 1346 and add:

    
    echo '<pre>';
    		print_r( $field );
    	echo '</pre>';
    	echo '<pre>';
    		print_r( $value );
    	echo '</pre>';
    	die;
    

    What data do you get?

    Thanks
    E

  • This reply has been marked as private.
  • Hi @gregattack

    Thanks for the debug info.
    It looks like the repeater field is found, and the $value is also correct.
    I’ll do some testing on my end and let you know what I find.

    Thanks
    E

  • Having the same issue. Please, please resolve this as soon as possible since a project I’m working on which needs this functionality goes live in a couple of days.

    It works great except when trying to update it (however, it seems to adjust the amount of rows accordingly, just not setting its value), even though we’re using the field key. One tiny bit of error I snapped up was “Invalid argument supplied for foreach()” on line 1440 in api.php.

  • Hi @OscarEriksen

    I’ll do some testing this morning, but can you make sure that your update_field code is run AFTER the init action?

    Thanks
    E

  • Hi @OscarEriksen

    I can confirm that the update_field function is working correctly with the repeater field!

    I just added a repeater field to a page, edited the page.php template and ran this code:

    
    $repeater = get_field('repeater');
    
    // append row
    $repeater[] = $repeater[0];
    
    update_field('repeater', $repeater);
    

    Looking back at the wp-admin edit page, I can see an extra row in the data!

    Could this issue be that you are running your code before the repeater field is being included (before init)?

    Thanks
    E

  • Hello,

    Works now – sorry but it probably was me being a bit too tired and irritated with WordPress. 🙂

    For future reference – if anyone else gets a warning about a foreach-loop not working in api.php, it’s probably because you aren’t really formatting your data in the way that the API wants to, which was the case for me.

    Best regards,

  • @elliot were you ever able to figure out my issue?

  • Hi @gregattack

    I did some testing and only had positive results.
    Could this issue be that you are running your code before the repeater field is being included (before init)?

    Thanks
    E

  • This reply has been marked as private.
  • Hi @gregattack

    The functions.php file is run BEFORE the init action. To hook into the init action, please read up on WP actions, and write a function and attach it to the init action. Something like this would work:

    
    function my_init() {
        // do something
    }
    add_action('init', 'my_init');
    

    Thanks
    E

  • Hi @gregattack

    How did you go with the init action?

    Thanks
    E

  • This reply has been marked as private.
  • Hi @gregattack

    You will need to be more descriptive with your questions. What CSS are you referring to?

    Perhaps you could open a support ticket:
    http://support.advancedcustomfields.com/

    Thanks
    E

  • Hi! Is there any other way to update(create) repeater field out of init action? I’m making an import script for posts and I can’t use it in the init action..

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

The topic ‘Repeater Field not playing well using update_field()’ is closed to new replies.