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 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) {
share|improve this question

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);
share|improve this answer
Awesome! Thank you! – Pete Jun 17 '11 at 14:11
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 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.