Title: pulsarcoder's Replies | WordPress.org

---

# pulsarcoder

  [  ](https://wordpress.org/support/users/pulsarcoder/)

 *   [Profile](https://wordpress.org/support/users/pulsarcoder/)
 *   [Topics Started](https://wordpress.org/support/users/pulsarcoder/topics/)
 *   [Replies Created](https://wordpress.org/support/users/pulsarcoder/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/pulsarcoder/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/pulsarcoder/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/pulsarcoder/engagements/)
 *   [Favorites](https://wordpress.org/support/users/pulsarcoder/favorites/)

 Search replies:

## Forum Replies Created

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

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[CSV Importer] [Plugin: CSV Importer] Overwrite posts](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-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;
           }
       ```
   
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[CSV Importer] [Plugin: CSV Importer] Overwrite posts](https://wordpress.org/support/topic/plugin-csv-importer-overwrite-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-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

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