Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.