Support

Account

Home Forums General Issues Inserting CSV data into custom fields

Solved

Inserting CSV data into custom fields

  • Hello all. I’m having an issue inserting CSV data into custom fields. My code is as follows:

    $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);
            }
        }
    }

    Before this block, I have an array of Field Keys called field_keys and $inventory is the array containing the CSV data I get from the fgetcsv() PHP function.

    The problem is with the update_field() function. If I replace $inventory[$i][$m] with $inventory[0][1] everything works. The first record is added into all of my fields. But I use i and m, nothing happens.

    Also, if I replace the update_field() function and just print $inventory[$i][$m], it prints the correct information.

    Any ideas??

  • May not be the problem, but you’re starting $i at 1

    
    for($i = 1, ...
    

    but you say that it works using

    
    $inventory[0][1]
    

    so shouldn’t $i start at 0?

    
    for($i = 0, 
    
  • 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.

  • When you say “it just hangs”, do you get any type of an error?

  • Not that I can find. When I run it in the terminal it doesn’t do anything and I have to press Ctrl+Z to stop it.

  • What if you remove comment the print and do both the print and update, does it continue looping and printing? How do you know that the update_field() call is not being executed?

  • When I print and update_field(), it is in an infinite loop for the print. It does create a post with the title of “test”, but none of the fields are populated.

  • when you say infinite loop, the same value, $inventory[$i][$m], continues to be printed?

  • The same set of values yes.

  • k, so that does not make any sense at all.

    Stepping back a step, how is this code called? Is it in a function? Is it run by some other action/hook?

    Something else I just noticed, but shouldn’t have anything to do with the infinite loop, shouldn’t each row ($i) be for a different post?

  • Right now it’s just a php script that is called when I manually call it. I will make a cron job for it so that it runs probably once a night or so.

    Yes each [$i] should be it’s own post. I’m sorry I didn’t make that clear when I posted at the beginning. And so far that isn’t working either. I know it has something to do with the ordering of the loop but I’m trying to understand it. I accidentally put wp_insert_post call in the wrong place and created over 300 posts that I had to delete. Oops. Haha. But when I did that, none of the fields were populated. So I know it has to be how I have things ordered. I really appreciate you walking through this with me to help me understand what’s going on.

  • I think I see what’s going on, maybe this will help

    
    for ($i = 1, $numInventory = count($inventory); $i < $numInventory; $i++) {
      
      // for each row of the input, create a new post
      $post_information = array(
          'post_title'    =>  'test '.$i,
          'post_type'     =>  'vehicle',
          'post_status'   =>  'publish'
      );
      $postID = wp_insert_post( $post_information );
      
      // the following assumes that the order of 
      // the columns in the csv matches
      // the order of the list of field keys
      
      for ($m = 0, $numSub = count($inventory[$i]); $m < $numSub; $m++) {
        
        // use the field key for $m column
        update_field($field_keys[$m], $inventory[$i][$m], $postID);
        
      } // end for $m
      
    } // end for $i
    
  • That works!!!

    There are a few posts that are empty though. I’m not sure why, but this at least puts me on the right direction. Thank you so much for your help!!!

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

The topic ‘Inserting CSV data into custom fields’ is closed to new replies.