3

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;
   $i++;
}

require('./wp-load.php');

foreach ($posts as $post) {
  wp_insert_post($post);
}

1 Answer 1

2

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);
3
  • 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? Commented Jun 17, 2011 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. Commented Jun 17, 2011 at 15:12
  • ok, I'll give it a try Commented Jun 17, 2011 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.