title: Importing +90K Posts to Custom Post Type in WordPress
body: <p>I'm currently working on importing 90K+ posts to a Custom Post Type including Taxonomies and Meta Values. Since I'm running into several issues and questions, I would like to ask the community if anyone has experience with importing that amount of posts.</p>
  <p>I read multiple posts about this topic, but i'm not 100% sure if I've found the good solution. My current setup for the import function is this:</p>
  <pre><code>function import() { 
      global $wpdb;
  
      if (!defined('WP_IMPORTING')) {
          define('WP_IMPORTING', true);
      }
      
      ini_set(&quot;memory_limit&quot;, -1);
      set_time_limit(0);
      ignore_user_abort(true);
      
      wp_defer_term_counting(true);
      wp_defer_comment_counting(true);
      
      $wpdb-&gt;query('SET autocommit = 0;');
      
      // register a function for execution on shutdown
      register_shutdown_function(function () {
          global $wpdb;
      
          $wpdb-&gt;query('COMMIT;');
          $wpdb-&gt;query('SET autocommit = 1;');
      
          wp_defer_term_counting(false);
          wp_defer_comment_counting(false);
      });
  
      foreach($x as $y =&gt; $z) {
          // check if current already exists
          $postExists = post_exists(…);
          
          if (!is_wp_error($postExists)) {
              $postId = wp_insert_post(…);
  
              // taxonomies
              if (isset(…)) {
                  $term = term_exists(…);
                  if($term) {
                      wp_set_post_terms(…);
                  }
              }
  
              // meta values
              // do one query for multiple meta values
              $metaKeys = [];
              $metaKeys['key'] = 'value';
              …
              $customFields = [];
              $placeHodlers = [];
              $queryString = &quot;INSERT INTO $wpdb-&gt;postmeta ( post_id, meta_key, meta_value) VALUES &quot;;
              foreach ($metaKeys as $key =&gt; $value) {
                  array_push($customFields, $postId, $key, $value);
                  $placeHodlers[] = &quot;('%d', '%s', '%s')&quot;;
              }
              $queryString .= implode(', ', $placeHodlers);
              $wpdb-&gt;query($wpdb-&gt;prepare(&quot;$queryString &quot;, $customFields));
          }
      }
      
      $wpdb-&gt;query('COMMIT;');
      $wpdb-&gt;query('SET autocommit = 1;');
      wp_defer_term_counting(false);
      wp_defer_comment_counting(false);
  }
  </code></pre>
  <p>The import still fails often with endless processing.</p>
  <p>Thanks!</p>