Home Forums Add-ons Repeater Field problems saving file type repeater field


problems saving file type repeater field

  • I’m having difficulties storing data for a file-type field inside a repeater field.

    I first store the file using wp_insert_attachment and get the attachment ID (no problem here). Then I construct my row array and add it to the repeater field, referencing the previously created post:

    $row = array('memos_upload_file' => $attach_id);
    $u = add_row('memos_attachments', $row, $post_id);

    It seems very straightforward — but the data is not saved. Is there any way to get more feedback besides the boolean success/fail?

  • Hi @bshaughnessy

    Could you please try to use the update_field() function instead? You should be able to do it like this:

    // Please use field key instead of field name
    $field_key = 'field_123456789abc';
    // Get the field value first if it's available
    $repeater_value = get_field('repeater_field_name');
    // Add the row to the repeater
    $repeater_value[] = array('memos_upload_file' => $attach_id);
    // Update the repeater field
    update_field($field_key, $repeater_value, $post_id );

    I hope this helps 🙂

  • That did it. I had to restructure a bit to get the correct flow for cycling through multiple file records, but it worked as you described.

    A few follow-up questions —

    1. why do we have to use the field key instead of the field name? I see no meaningful distinction between them, except that the field key is more cumbersome to work with.

    2. why didn’t the add_row() (or update_row()) function work? based on the documentation, I should have been able to achieve what I needed using that function (which seems simpler and more straightforward than your solution).

  • Hi @bshaughnessy

    I’m glad that worked for you.

    Regarding your questions:

    1. ACF saves two entries in the database when you save a custom field, the value and the reference. This mostly happens when you save a post from the backend. The value looks like this:

    field_name : field value

    While the reference looks like this:

    _field_name : field_1234567890abc

    The reference is important for ACF to know which field you want to save/update exactly. That’s because you can have multiple fields with the same name on a single page, but you can’t have multiple fields with the same field key.

    When you create a post programmatically, the reference is not created automatically. So, you need to use the field key instead of the field name. That way ACF know which field you want to save/update.

    2. Yes, the add_row() should be working. I think the issue is the same with the first answer. If you want, you can try to use that function again and use the field key instead of the field name for the parent selector.

    I showed you the update_field() function because it’s the most basic one and it should show the saving process more clearly.

    I hope this helps 🙂

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

The topic ‘problems saving file type repeater field’ is closed to new replies.