• Hello,

    I have a CVS file with almost 5000 records and I want to insert it into custom post with custom fields (meta). I have a column (attachment) image type and I want to insert and attach image into the post.

    I have all images in uploads/2015/05 folder.

    So far my code:

    `$file = fopen( get_template_directory() . ‘/data.csv’, ‘r’);
    while (($line = fgetcsv($file)) !== FALSE) {
    $title = $line[0];
    $content = $line[2];
    $time = get_the_time(‘l, F jS, Y’);
    $my_post = array(
    ‘post_title’ => $title,
    ‘post_date’ => $time,
    ‘post_content’ => $content,
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘abc’,
    );
    $the_post_id = wp_insert_post( $my_post );
    __update_post_meta( $the_post_id, ‘abc’, $abc);
    $file = $line[3]; // abc.png
    // Here I want to attach the attachment the image abc.png where its location in uploads/2015/05 folder.

    Please guide.

    Thanks,
    Noor

  • The topic ‘Attach image from a folder while inserting post from CSV file’ is closed to new replies.