Support

Account

Home Forums Add-ons Repeater Field How do I set the value in a repeater sub field of type file?

Solved

How do I set the value in a repeater sub field of type file?

  • I have a repeater field group set up with the following sub fields

    1. accessory_name – type=text
    2. accessory_file – type=file
    3. accessory_description – type=Wysiwyg Editor

    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

  • Okay, what works is when you assign the post ID of an attachment type post for the media that was uploaded.

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

The topic ‘How do I set the value in a repeater sub field of type file?’ is closed to new replies.