Support

Account

Home Forums Backend Issues (wp-admin) ACF file field with wp_insert_post function

Solving

ACF file field with wp_insert_post function

  • hello, I need to create multiple php posts using the wp_insert_post function.
    how can I integrate an acf file field into the code, inserting a url file that I get from a string variable php? Thank you very much

  • You need to insert the file with something like this https://gist.github.com/hissy/7352933

    Once that is done use the attachment ID to update the ACF field.

  • thank you for this, but i don’t undestand how i can integrate with
    wp_insert_post function.
    Thanks a lot

  • i try to modify these functions (get here)
    https://www.pearlbells.co.uk/insert-update-acf-image-repeater-field-programmatically/

    function uploadImages( $files ) {
     
        $imgAcfIds = array();
     
        foreach($files as $imgfile) {
            $filename = basename($imgfile);
            $upload_file = wp_upload_bits($filename, null, file_get_contents($imgfile));
            if (!$upload_file['error']) {
                $wp_filetype = wp_check_filetype($filename, null );
                $attachment = array(
                    'post_mime_type' => $wp_filetype['type'],
                    'post_parent' => 251,
                    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
                    'post_content' => '',
                    'post_status' => 'inherit',
    				'post_author'   => 2,
    	'post_category' => array(9)
    	
                );
    			
    			
                $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 251 );
                
    			
                // get attachment id for acf field
     
                $imgAcfIds[] = array('documento' => $attachment_id);
                echo 'Attachment Id :'.$attachment_id .PHP_EOL;
                if (!is_wp_error($attachment_id)) {
                    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                    $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
                    wp_update_attachment_metadata( $attachment_id,  $attachment_data );
                }
            }
        }
        echo '<pre>';
        print_r($imgAcfIds);
    	$post_id = wp_insert_post( wp_slash($attachment) );
        update_field( 'field_5d78c5acf0ff3', $imgAcfIds , 251 );
    	
    }
    
        function readDataFromImageFolder() {
            
            $imageFolderName = 14;
            $base = dirname(__FILE__);
            $dirname = $base.'/images/'.$imageFolderName;
            $files = array();
            
            if (!file_exists($dirname)) {
                echo "The directory $dirname not exists.".PHP_EOL;
                exit;
            } else {
                echo "The directory $dirname exists.".PHP_EOL;
                $dh  = opendir( $dirname );
                
                while (false !== ($filename = readdir($dh))) {
                    if ($filename === '.' || $filename === '..') continue;
                    $files[] = $dirname.$filename;
                }
                uploadImages( $files );
            }
         
          //  echo '<pre>';
          //  print_r($files);die();
         
        }
    

    but don’t work my file field name is documento my file field key is field_5d78c5acf0ff3a

    thanks a lot

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

The topic ‘ACF file field with wp_insert_post function’ is closed to new replies.