Support

Account

Home Forums Add-ons Repeater Field Repeater rows don't save new files

Solved

Repeater rows don't save new files

  • Hey everyone. I’m working on a project right now that uses a repeater row on a custom post object to store information about an uploaded file. I use built-in WordPress functionality to upload the file, attach meta data, and then attach it to the post. This works just fine, and no matter what happens, it seems to work as expected. I then use this code to add a row to the repeater:

    
    $field_key = "documents";
    
    $value = get_field($field_key, $post_ID);
    
    $value[] = array("file" => $attach_id, "file_description" => "", "file_user" => $userID, "file_date" => $date, "file_phase" => $phaseID);
    
    update_field( $field_key, $value, $post_ID );
    

    (Obviously much of that info is related specifically to my project.) So here’s the problem – the first time I do this, it works great. Exactly as expected. The file is attached to the row and the other information is attached correctly as well.

    However, on subsequent uploads, the previous files disappear. The files themselves are still attached to the custom post object, and can still be found in the Media in WordPress. However, the rows themselves just vanish the file that was saved to it. All the other data is saved to the row. It’s only the file that disappears. So the result is the only row with a file attached to it is the one that I just barely uploaded.

    Is this a bug, or am I doing something wrong?

  • Hi @twistysnacks

    Can you please perform some debugging to find the issue?

    First save a file (I am to assume this works correctly for the first row)

    Now edit your code to print the variable before saving like so:

    
    $field_key = "documents";
    
    $value = get_field($field_key, $post_ID);
    
    echo '<pre>';
    	print_r(value);
    echo '</pre>';
    die;
    

    What is the $value?

    I think the issue may be fixed by changing $field_key to actually use the field key, not the field name

  • I actually was using the field key at first. I switched to “documents” in an effort to debug. I put it back to the field key for the below test. Anyway, printing the $value array gives me back an array with all the rows of the repeater, as expected.

    Array
    (
        [0] => Array
            (
                [file] =>
                [file_description] => 
                [file_user] =>
                [file_date] =>
                [file_phase] =>
            )
    )

    Sorry, truncated the values for space. The values were all exactly as expected for the existing file. A URL for the file itself, text for description, User object for user, @ format for date, and WP Object for phase. Totally good.

    Doing the same thing after the $value[], however, shows the above row correctly, and then the row I am trying to add like this:

    
        [2] => Array
            (
                [file] => 414
                [file_description] => 
                [file_user] => 1
                [file_date] => 1374226973000
                [file_phase] => 232
            )

    So instead of objects and URLs, it’s showing the ID of the item I am about to insert, I guess. Is this appropriate? Just making sure.

    I’m kind of at a loss. It’s still deleting the previous file ID. Everything else remains when I add a row – description, user, date, phase. The only thing deleted from previous rows is the file.

  • HI @twistysnacks

    aah, I think I understand the issue now.

    When saving a value, the value MUST be in the correct format for saving. This means, images / files muct be saved as an attachment ID.

    If you are trying o save a URL as the value, ACF will fail to save the data entirely.

    You will need to change the return type of your file field to return the ID, or Object. These can be saved, not the URL

  • I can’t believe I missed that!! Thank you again Elliot.

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

The topic ‘Repeater rows don't save new files’ is closed to new replies.