Home Forums General Issues Inserting CSV data into custom fields


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


    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.

    function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle) ) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        return $line_of_text;
    $csvFile = 'data/dtas-inventory.csv';
    $inventory = readCSV($csvFile);
    $field_keys = array(
    $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)

You must be logged in to reply to this topic.