I have created an import file to get the articles from a non-wordpress db and bring them in as posts in wordpress, I am not sure how to bring over the thumbnail image into my custom field I defined as "thumbnail". Any suggestions.

Here is the code I am using:

    $results = mysql_query("SELECT headline, abstract, article, author, posted,    category, img, facilities FROM articles",$lclink);

    $i = 0;
while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) {
   $post = array();
   $post['post_status'] = 'publish';
   $post['post_category'] = array(1);
   $post['post_date'] = date('Y-m-d H:i:s',strtotime($row['posted']));
   $post['post_title'] = $row['headline'];
   $post['post_excerpt'] = $row['abstract'];
   $post['post_content'] = $row['article'];
   $post['post_author'] = $row['author'];

   $posts[$i] = $post;


foreach ($posts as $post) {

you need to get the ID back from your post insertion and use that with add_post_meta:

$post_id = wp_insert_post($post);
add_post_meta($post_id, 'thumbnail', $yourvalue, true);
  • So the code you gave should work to assign the retrieved data once I have it, any suggestions on writing the code for actually retrieving that data from the existing database? – Pete Jun 17 '11 at 14:37
  • @Pete - is the field in that original query? you could just do all of the work in that first while loop rather than building the $posts array and looping through that separately. – Milo Jun 17 '11 at 15:12
  • ok, I'll give it a try – Pete Jun 17 '11 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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