Title: Imported csv file won&#039;t update posts using wp_insert_post()
Last modified: August 22, 2016

---

# Imported csv file won't update posts using wp_insert_post()

 *  [static-blur](https://wordpress.org/support/users/static-blur/)
 * (@static-blur)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/imported-csv-file-wont-update-posts-using-wp_insert_post/)
 * Hi
 * Long time lurker here on the forum.
    Normally an hour or so searching and I find
   a solution to my issue, but this time I’ve had no luck.
 * I’m using the code below to upload csv file and import the posts into the custom
   post type, everything is working except I’m unable to figure out how to get wp_insert_post()
   to update a published post which shares the same ID.
 * Instead it just duplicates a new post, I’m using import_id to grab the id from
   the csv column which works if the id doesn’t exist. But updating is where I’m
   stuck.
 * Any tips or advice in this area would be much appreciated.
 *     ```
       <?php $file = wp_import_handle_upload();
   
       	if(isset($file['file'])) {
   
       		$row = 0;
       		if (($handle = fopen($file['file'], "r")) !== FALSE) {
       		    while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
   
       		    	if($row == 0) {
       		    		// Don't do anything with the header row
       		    		$row++;
       		    		continue;
       		    	} else {
       		    		$row++;
   
       		// Get the post data
       	$postinfo = array (
       		'import_id'	=> $data[0],
       		'post_type'     => '$post_type',
       		'post_title'    => $data[2] .' ('. $data[1].')',
       		'post_content' 	=> $data[3],
       		'post_status'   => 'publish',
       			);
   
        $newpost = wp_insert_post($postinfo);
       }?>
       ```
   

Viewing 1 replies (of 1 total)

 *  [msoftware](https://wordpress.org/support/users/msoftware/)
 * (@msoftware)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/imported-csv-file-wont-update-posts-using-wp_insert_post/#post-5490999)
 * Hi,
 * you can use the seconp parameter of the wp_insert_post function to see the error.
 * $wp_error (bool) (optional) Allow return of WP_Error object on failure
    Default:
   false
 * You can find a working example of wp_insert_post error handling at [http://vanillaco.de/file.php?name=%2Floudervoice%2Ftrunk%2Finc%2Fldv-ajax-functions.php#35-42](http://vanillaco.de/file.php?name=%2Floudervoice%2Ftrunk%2Finc%2Fldv-ajax-functions.php#35-42)

Viewing 1 replies (of 1 total)

The topic ‘Imported csv file won't update posts using wp_insert_post()’ is closed
to new replies.

## Tags

 * [csv](https://wordpress.org/support/topic-tag/csv/)
 * [importing](https://wordpress.org/support/topic-tag/importing/)
 * [wp_insert_post](https://wordpress.org/support/topic-tag/wp_insert_post/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [msoftware](https://wordpress.org/support/users/msoftware/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/imported-csv-file-wont-update-posts-using-wp_insert_post/#post-5490999)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
