Support

Account

Home Forums General Issues Inserting CSV data into custom fields Reply To: Inserting CSV data into custom fields

  • Well it works regardless of the numbers in the brackets (as long as it exists in the array of course.)

    The reason I started with zero was because the headers are in the first array and I wanted to bypass those.

    This is my full code.

    <?php
    
    function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle) ) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
        return $line_of_text;
    }
    
    $csvFile = 'data/dtas-inventory.csv';
    
    $inventory = readCSV($csvFile);
    
    $field_keys = array(
        'field_58c95667b0f7d',
        'field_58c956c86747a',
        'field_58c9576a1f775',
        'field_58c957951f776',
        'field_58c9581639a32',
        'field_58c9582739a33',
        'field_58c9583339a34',
        'field_58c9583d39a35',
        'field_58cc131c5f7ac',
        'field_58c958ad3dae4',
        'field_58c958b53dae5',
        'field_58c958bb3dae6',
        'field_58c958c63dae7',
        'field_58c958dc942c8',
        'field_58cc138e09213',
        'field_58c958f1942c9',
        'field_58cc13ba6995a',
        'field_58c95901942ca',
        'field_58c959189521f',
        'field_58c9592195220',
        'field_58c9592795221',
        'field_58c9593195222',
        'field_58c9594495223',
        'field_58c9596595224',
        'field_58c959dd2451a',
        'field_58cc13f66995b',
        'field_58cc140c6995c',
        'field_58cc141c6995d',
        'field_58c95a102451b',
        'field_58cc142b6995e',
        'field_58cc143b6995f',
        'field_58cc145169960',
        'field_58c95a2d3ded4',
        'field_58cc146269961',
        'field_58cc146e69962',
        'field_58cc147869963'
    );
    
    $post_information = array(
        'post_title'    =>  'test',
        'post_type'     =>  'vehicle',
        'post_status'   =>  'publish'
    );
    
    $postID = wp_insert_post( $post_information );
    
    foreach($field_keys as $field_key) {
        for ($i = 1, $numInventory = count($inventory); $i < $numInventory; $i++) {
            for ($m = 0, $numSub = count($inventory[$i]); $m < $numSub; $m++) {
                update_field($field_key, $inventory[$i][$m], $postID);
                //print "Record [$i][$m] is " . $inventory[$i][$m] . "\n";
            }
        }
    }
    ?>

    So if I comment out the updatefield() function and uncomment the print line, it prints what is expected to the terminal. But when I use the update_field() function and run the php script in the terminal it just hangs. But if I replace $inventory[$i][$m] with $inventory[2][5] (or anything that exists) it goes straight in the correct fields.

    I’m really at a loss as to why it isn’t working.