importing data into wordpress db

I’ve run into this issue before when needing to import a large dataset into WordPress. The importers start failing. What I suggest is creating a custom PHP script that reads the values from the non-WP database and then inserts them into WordPress using the WordPress functions. This way, we ensure all the background data processing happens in WordPress.

The first thing is to setup the script. We are going to run the script from the command line because it will not run into the timeout restraints that you normally get when PHP runs on a webpage.

In your WordPress root directory, create a new file import-data.php:

<?php
// Include WordPress functions
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

echo "It Works\n";

From the command line, go to your WordPress directory and run: php import-data.php. You should see It Works printed out.

From here, I can only give you general help with the rest of the process as I don’t know the schematic of your data.

Now you’ll want to do you own MySQL query to your non-WP database

<?php
// Include WordPress functions
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

// Do you MySQL database connection and query here
$original_data = array(/* ... placeholder */);

Assuming each record will be a post with custom field data. You’ll need to first create the post object which has the basic fields such as title and content. It also has the fields that you’ll want to manually set such as post_type if you are using a custom post_type or the post_status to publish it by default.

After you create the post object, you’ll get the post ID which you’ll need to insert the custom fields.

foreach ($original_data as $data) {
   // Useful to see where we are in the import process
  echo "Inserting record ", $data['id'], "\n";

  // First create the new post object
  // See: http://codex.wordpress.org/Function_Reference/wp_insert_post
  $post_data = array( 'post_title' => $data['name'], 'post_type' => 'product', 'post_status' => 'publish' );
  $post_id = wp_insert_post( $post_data );

  // Insert custom post data
  // See: http://codex.wordpress.org/Function_Reference/add_post_meta
  add_post_meta($post_id, 'price', $data['Price']);
  add_post_meta($post_id, 'quantity', $data['q']);

  // What if you also want taxonomy terms?
  // See: http://codex.wordpress.org/Function_Reference/wp_set_post_terms
  wp_set_post_terms( $post_id, array($data['style']), 'styles' );
}
echo "Finished\n";

Hopefully this helps you figure out how to import your data.