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

Currently I'm working on a project where site is driven by user articles, how it works:

  1. Any user goes to compose post page and writes post along with few details, has upload file option for image and then submits
  2. Post is uploaded in wordpress and the image attached is set as featured image
  3. Admin approves the post and it gets published.

Here user is not required to have an account. I am using wp_insert_post to create a new post but not able to figure out how to add featured image via PHP.

share|improve this question
does it have to be added via .php or could you consider a plugin? – MartinJJ Jun 21 '11 at 11:15
vudu.me/6n is a writeup I did a bit ago, I think it may be what you are looking for? – Rev. Voodoo Jun 21 '11 at 11:27
I would prefer php code because the plugin itself would be in php. Custom written code is more secure and efficient – Tarun Jun 21 '11 at 11:33
up vote 3 down vote accepted

Use the set_post_thumbnail function.

set_post_thumbnail( $post_ID, $thumbnail_id );

Require you use WordPress 3.1.0 or later.

You need call this function after you have successfully created your post via wp_insert_post and have a valid $post_ID.

share|improve this answer
Hmm... that's one of them things I missed in 3.1. Thanks for pointing that out! – Rev. Voodoo Jun 21 '11 at 12:33
Thanks :) will use it – Tarun Jun 22 '11 at 11:48

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.