Home › Forums › Add-ons › Repeater Field › How do I set a File field from the acf api?
I have a repeater field group set up with the following sub fields
I am creating posts that use this repeater group and I am able to set values into both the sub accessory_name and accessory_description fields just fine, but when I try to add a filename to the accessory_file I can see when I am in my code debugger that I have assigned a filename string to the sub_field, but when I edit the post in WordPress the field shows as empty.
Just to explain how the files are added:-
In the process of creating the post I upload files for each of the accessories (pdf’s, mp3’s, jpg’s etc) and rename them into wp_upload_dir(). I then create an attachment post. This is done as follows:
if (rename($from, $to)) {
// If the rename worked then create an attachment for it and make the parent the collection post
// Prepare an array of post data for the attachment.
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( $file['name'], null );
$post_title = preg_replace( '/\.[^.]+$/', '', $file['name'] );
$attachment_params = array(
'guid' => $wp_upload_dir_info['url'] . '/' . $file['name'],
'post_mime_type' => $filetype['type'],
'post_title' => $post_title,
'post_content' => '',
'post_status' => 'inherit'
);
I have a repeater field group set up with the following sub fields
<ol>
<li>accessory_name - type=text</li>
<li>accessory_file - type=file</li>
<li>accessory_description - type=Wysiwyg Editor</li>
</ol>
I am creating posts that use this repeater group and I am able to set values into both the sub accessory_name and accessory_description fields just fine, but when I try to add a filename to the accessory_file I can see when I am in my code debugger that I have assigned a filename string to the sub_field, but when I edit the post in WordPress the field shows as empty.
Just to explain how the files are added:-
In the process of creating the post I upload files for each of the accessories (pdf's, mp3's, jpg's etc) and rename them into wp_upload_dir(). I then create an attachment post. This is done as follows:
if (rename($from, $to)) {
// If the rename worked then create an attachment for it and make the parent the collection post
// Prepare an array of post data for the attachment.
// Check the type of file. We’ll use this as the ‘post_mime_type’.
$filetype = wp_check_filetype( $file[‘name’], null );
$post_title = preg_replace( ‘/\.[^.]+$/’, ”, $file[‘name’] );
$attachment_params = array(
‘guid’ => $wp_upload_dir_info[‘url’] . ‘/’ . $file[‘name’],
‘post_mime_type’ => $filetype[‘type’],
‘post_title’ => $post_title,
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);
add_row(‘nh_book_accessory’, $row, $postID);
I then add the value to the accessory_file like this:
$row = array(
‘attachment_post_id’ => $attachID,
‘accessory_name’ => $post_title,
‘accessory_file’ => $file[‘name’],
‘accessory_description’ => $accessory_description
);
add_row(‘nh_book_accessory’, $row, $postID);
Can anyone tell me what I am doing wrong, or maybe what I'm missing.
Possibly I should be putting the attachment guid in this field? T think the documentation for setting a file type 'file' could do with having some info on this.
Thanks,
Nick
I then add the value to the accessory_file like this:
$row = array(
'attachment_post_id' => $attachID,
'accessory_name' => $post_title,
'accessory_file' => $file['name'],
'accessory_description' => $accessory_description
);
add_row('nh_book_accessory', $row, $postID);
Can anyone tell me what I am doing wrong, or maybe what I’m missing.
Possibly I should be putting the attachment guid in this field? T think the documentation for setting a file type ‘file’ could do with having some info on this.
Thanks,
Nick
The file field must hold the attachment ID for the attachment that you inserted.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
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.