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.

  • does it have to be added via .php or could you consider a plugin?
    – MartinJJ
    Commented Jun 21, 2011 at 11:15
  • vudu.me/6n is a writeup I did a bit ago, I think it may be what you are looking for? Commented Jun 21, 2011 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
    Commented Jun 21, 2011 at 11:33

1 Answer 1


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.

  • Hmm... that's one of them things I missed in 3.1. Thanks for pointing that out! Commented Jun 21, 2011 at 12:33

Your Answer

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

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