Title: [Plugin: CSV Importer] Overwrite posts
Last modified: August 20, 2016

---

# [Plugin: CSV Importer] Overwrite posts

 *  Resolved [fkjason](https://wordpress.org/support/users/fkjason/)
 * (@fkjason)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/)
 * Hi! Is there a way to use this plugin to overwrite posts? I need to mass update
   taxonomies and descriptions from existing posts but, if I use this plugin, it
   duplicates them
 * example
 * myweb.com/post-slug
 * when I import another post with this slug, it generates:
 * myweb.com/post-slug-2
 * [http://wordpress.org/extend/plugins/csv-importer/](http://wordpress.org/extend/plugins/csv-importer/)

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893383)
 * Yes, see [this other thread](http://wordpress.org/support/topic/plugin-csv-importer-is-it-possible-also-to-update-posts-with-this-plugin)
   for more information.
    That thread is closed now, and I never put the details
   of the minor code changes needed. Starting with version 0.3.7 as a base, make
   3 changes in csv_importer.php as follows: Before Line 277 add:
 *     ```
       if (!empty($new_post['ID']))
               $id = wp_update_post($new_post);
       	else
       ```
   
 * at Line 254 add:
    `'ID' => convert_chars(trim($data['csv_post_id'])),`
 * at Line 40 add:
    `'csv_post_id' => null,`
 * Do it in that order, so the line numbers are right.
 * You can get a nice export file to work with using phpMyAdmin or try this [exporter](http://extend.thecartpress.com/ecommerce-plugins/csv-export/).
 *  [pulsarcoder](https://wordpress.org/support/users/pulsarcoder/)
 * (@pulsarcoder)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893385)
 * Hi,
 *  I solved this issue in my project,
 *  Solution:
    Just changed the create_post function in the csv-importer.php file,
 *     ```
       function create_post($data, $options) {
               extract($options);
       	<strong>global $wpdb;</strong>  //edited by guru
   
               $data = array_merge($this->defaults, $data);
               $type = $data['csv_post_type'] ? $data['csv_post_type'] : 'post';
               $valid_type = (function_exists('post_type_exists') &&
                   post_type_exists($type)) || in_array($type, array('post', 'page'));
   
               if (!$valid_type) {
                   $this->log['error']["type-{$type}"] = sprintf(
                       'Unknown post type "%s".', $type);
               }
   
               $new_post = array(
                   'post_title'   => convert_chars($data['csv_post_title']),
                   'post_content' => wpautop(convert_chars($data['csv_post_post'])),
                   'post_status'  => $opt_draft,
                   'post_type'    => $type,
                   'post_date'    => $this->parse_date($data['csv_post_date']),
                   'post_excerpt' => convert_chars($data['csv_post_excerpt']),
                   'post_name'    => $data['csv_post_slug'],
                   'post_author'  => $this->get_auth_id($data['csv_post_author']),
                   'tax_input'    => $this->get_taxonomies($data),
                   'post_parent'  => $data['csv_post_parent'],
               );
   
       		<strong>// edited by guru
       		$new_post['ID'] = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $data['csv_post_title'] . "'" );
       		// ends</strong>
   
               // pages don't have tags or categories
               if ('page' !== $type) {
                   $new_post['tags_input'] = $data['csv_post_tags'];
   
                   // Setup categories before inserting - this should make insertion
                   // faster, but I don't exactly remember why :) Most likely because
                   // we don't assign default cat to post when csv_post_categories
                   // is not empty.
                   $cats = $this->create_or_get_categories($data, $opt_cat);
                   $new_post['post_category'] = $cats['post'];
               }
   
               <strong>// create or update edited by guru!
               if(!empty($new_post['ID'])) {
       			wp_update_post($new_post);
       		} else {
       			wp_insert_post($new_post);
       		}
       		// ends</strong>
   
               if ('page' !== $type && !$id) {
                   // cleanup new categories on failure
                   foreach ($cats['cleanup'] as $c) {
                       wp_delete_term($c, 'category');
                   }
               }
               return $id;
           }
       ```
   
 * _[Moderator Note: Please post code or markup snippets between backticks or use
   the code button. Or better still – use the [pastebin](http://wordpress.pastebin.com/).
   As it stands, your code may now have been permanently damaged/corrupted by the
   forum’s parser.]_
 *  just copy the above function create_post and replace the one in the csv-importer.
   php at line no 239
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893386)
 * [@pulsarcoder](https://wordpress.org/support/users/pulsarcoder/), your code is
   missing the assignment of the $id for the new post. No telling what else is wrong.
   
   Mine has been tested to work both with and without replacing the posts.
 *  [pulsarcoder](https://wordpress.org/support/users/pulsarcoder/)
 * (@pulsarcoder)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893387)
 * Hi
 * Thanks for your reply, please use the following code,
    $id is the return value
   of wp_update_post or wp_insert_post.
 * Actually in my project, i have so many posts also i have to update the post id
   in the excel as csv_post_id. instead i just changed the code to take the post
   title and find the id from the db.
    If exists the post is overwritten else inserted
   as new post.
 * please kindly post your feedback.
 *     ```
       function create_post($data, $options) {
               extract($options);
       	<strong>global $wpdb;</strong>  //edited by guru
   
               $data = array_merge($this->defaults, $data);
               $type = $data['csv_post_type'] ? $data['csv_post_type'] : 'post';
               $valid_type = (function_exists('post_type_exists') &&
                   post_type_exists($type)) || in_array($type, array('post', 'page'));
   
               if (!$valid_type) {
                   $this->log['error']["type-{$type}"] = sprintf(
                       'Unknown post type "%s".', $type);
               }
   
               $new_post = array(
                   'post_title'   => convert_chars($data['csv_post_title']),
                   'post_content' => wpautop(convert_chars($data['csv_post_post'])),
                   'post_status'  => $opt_draft,
                   'post_type'    => $type,
                   'post_date'    => $this->parse_date($data['csv_post_date']),
                   'post_excerpt' => convert_chars($data['csv_post_excerpt']),
                   'post_name'    => $data['csv_post_slug'],
                   'post_author'  => $this->get_auth_id($data['csv_post_author']),
                   'tax_input'    => $this->get_taxonomies($data),
                   'post_parent'  => $data['csv_post_parent'],
               );
   
       		<strong>// edited by guru
       		$new_post['ID'] = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $data['csv_post_title'] . "'" );
       		// ends</strong>
   
               // pages don't have tags or categories
               if ('page' !== $type) {
                   $new_post['tags_input'] = $data['csv_post_tags'];
   
                   // Setup categories before inserting - this should make insertion
                   // faster, but I don't exactly remember why :) Most likely because
                   // we don't assign default cat to post when csv_post_categories
                   // is not empty.
                   $cats = $this->create_or_get_categories($data, $opt_cat);
                   $new_post['post_category'] = $cats['post'];
               }
   
               <strong>// create or update edited by guru!
               if(!empty($new_post['ID'])) {
       		$id = wp_update_post($new_post);
       	} else {
       		$id = wp_insert_post($new_post);
       	}
       	// ends</strong>
   
               if ('page' !== $type && !$id) {
                   // cleanup new categories on failure
                   foreach ($cats['cleanup'] as $c) {
                       wp_delete_term($c, 'category');
                   }
               }
               return $id;
           }
       ```
   
 *  [bheadrick](https://wordpress.org/support/users/bheadrick/)
 * (@bheadrick)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893399)
 * This looks great! I’ll add this to [my fork of the plugin](https://github.com/BHEADRICK/csv-importer)(
   pretty soon, I’ll release it as a separate plugin).
 *  [surfrunner](https://wordpress.org/support/users/surfrunner/)
 * (@surfrunner)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893400)
 * Hi Joy, thanks for the perfect instructions on adding those lines of code. Works
   great on overwriting a post.
 * One question for you. How could I ensure that the custom fields also get overwritten?
   When I use your code it adds a while new custom field with the updated values.
 * Any suggestions would be greatly appreciated.
 * Cheers.
 * Jason
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893401)
 * Ah surfrunner, you are correct that the custom fields were not taken into account
   for updates. Neither were comments.
    But WordPress has it covered.
 * Search the code for add_post_meta and change it to update_post_meta. That function
   will call add if it doesn’t exist. (this could be a problem if you have multiple
   custom fields with the same name, such as image attachments…I haven’t tried it)
 * For comments, search for wp_insert_comment and change it to wp_update_comment.
   There might be more filtering going on than before, using that.
 *  [surfrunner](https://wordpress.org/support/users/surfrunner/)
 * (@surfrunner)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893402)
 * Thanks Joy.
 * That update worked, the custom fields are overwritten versus copied (very cool).
 * It does not work when I try to use it with pages that have 12+ custom fields…
   I get an error on import claiming some fields may be empty.
 * Kind of weird that it works if I import just the basics but when the .csv gets
   a bit more complicated it will not import (not because there are multiple custom
   fields with the same name though, I removed those and still get the error).
 * But thanks! I’ll keep tweaking and get it to work somehow. I appreciate your 
   quick response.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘[Plugin: CSV Importer] Overwrite posts’ is closed to new replies.

 * ![](https://ps.w.org/csv-importer/assets/icon-256x256.png?rev=3039593)
 * [CSV Importer](https://wordpress.org/plugins/csv-importer/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/csv-importer/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/csv-importer/)
 * [Active Topics](https://wordpress.org/support/plugin/csv-importer/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/csv-importer/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/csv-importer/reviews/)

 * 8 replies
 * 5 participants
 * Last reply from: [surfrunner](https://wordpress.org/support/users/surfrunner/)
 * Last activity: [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-posts/#post-2893402)
 * Status: resolved