title: Wordpress csv import duplicate
body: <p>I've used an adapted version of a csv import plugin for wordpress. It imports the csv to create and update posts. To update existing posts I've added an edit that finds the post id by title, then if it exists it overwrites, otherwise it creates a new post.</p>
  
  <p>Problem is that it seems to not be very reliable! If I import the same csv several times it over-writes most of them but I get a few duplicates. Is there any way I can make this more reliable, or is there another way I can deal with duplicate posts? </p>
  
  <p>main function for creating posts here</p>
  
  <pre><code> function create_post($data, $options) {
      extract($options);
  //edit 1 added here
     global $wpdb;  
  //end
  
      $data = array_merge($this-&gt;defaults, $data);
      $type = $data['csv_post_type'] ? $data['csv_post_type'] : 'post';
      $valid_type = (function_exists('post_type_exists') &amp;&amp;
          post_type_exists($type)) || in_array($type, array('post', 'page'));
  
      if (!$valid_type) {
          $this-&gt;log['error']["type-{$type}"] = sprintf(
              'Unknown post type "%s".', $type);
      }
  
      $new_post = array(
          'post_title'   =&gt; convert_chars($data['csv_post_title']),
          'post_content' =&gt; wpautop(convert_chars($data['csv_post_post'])),
          'post_status'  =&gt; $opt_draft,
          'post_type'    =&gt; $type,
          'post_date'    =&gt; $this-&gt;parse_date($data['csv_post_date']),
          'post_excerpt' =&gt; convert_chars($data['csv_post_excerpt']),
          'post_name'    =&gt; $data['csv_post_slug'],
          'post_author'  =&gt; $this-&gt;get_auth_id($data['csv_post_author']),
          'tax_input'    =&gt; $this-&gt;get_taxonomies($data),
          'post_parent'  =&gt; $data['csv_post_parent'],
      );
  
  // edit 2 here
      $new_post['ID'] = $wpdb-&gt;get_var( "SELECT ID FROM $wpdb-&gt;posts WHERE post_title = '" .   $data['csv_post_title'] . "'" );
  // ends
  
      // pages don't have tags or categories
      if ('page' !== $type) {
          $new_post['tags_input'] = $data['csv_post_tags'];
  
          // Setup categories before inserting 
          $cats = $this-&gt;create_or_get_categories($data, $opt_cat);
          $new_post['post_category'] = $cats['post'];
      }
  
  // edit 3
      if(!empty($new_post['ID'])) {
      $id = wp_update_post($new_post);
  } else {
      $id = wp_insert_post($new_post);
  }
  // ends
  
      if ('page' !== $type &amp;&amp; !$id) {
          // cleanup new categories on failure
          foreach ($cats['cleanup'] as $c) {
              wp_delete_term($c, 'category');
          }
      }
      return $id;
  }
  </code></pre>
  
  <p>thanks</p>