WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm migrating data from a totally different database style into WP. The main issue I'm trying to wrap my head around is how to import any meta data, given that WP uses a key value pair style for meta tables.

Is there a way to import column data as key value pairs?

share|improve this question
What is confusing or difficult about key/value metadata? – s_ha_dum Nov 30 '12 at 4:25
up vote 0 down vote accepted

You should wirte a Function or Plugin inside WordPress to achive that.

First, you could insert the tables into your WordPress Database.

After that, make sure you make good use of the WordPress postsstructure. Set up a Loop through your existing Posts that you want to import.

At each Post create a WordPress Post using wp_insert_post(). This Function returns the ID of your new Post.

Every bit of Metadata that you want to add to your Post is added afterwards. It would be like this:

foreach ( $existingposts as $existingpost ) {

    $newPostData['post_date'] = $existingpost['post_date'];
    // and so on, make sure to populate all the fields except the ID parameter, as this one is given by WordPress

    $newPostID = wp_insert_post( $newPostData );

    add_post_meta( $newPostID, '_firstPostMeta', $existingpost['firstPostMeta'] );
    // and so on, every Meta at a time


Be sure to test your function before looping it through all the posts at once. You may want to divide your stack of existing posts into groups of 20 or 50, depending what your server is capable to do, to avoid inconsitensies due to server timeout.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.