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'm dynamically creating a custom post type in code (not in the editor) and I have an image I want to attach to my custom post type as a post thumbnail, and have it resized, etc. How do I do that. I'm successfully creating the custom post type using wp_insert_post().

I've looked at wp_save_image(), wp_save_image_file(), load_image_to_edit(), get_attached_file() and others, but they all seem to require an image in $_POST. I also looked at wp_insert_attachment() and update_attached_file() which seemed promising, but were not image centric.

share|improve this question
up vote 2 down vote accepted

WP 3.1 introduces set_post_thumbnail() function, that sets attachment (that you will need to create first, you seem to be on right track with wp_insert_atachment()) as thumbnail for the post.

share|improve this answer
thanks the codex page you referenced for wp_insert_attachment() provided an excellent example. I used to only read codex pages, then recently I started to just read the core code - I guess both are the the best! – dwenaus Jun 1 '11 at 7:08

Unfortunately I don't think you can with wp_insert_post, but you can add the images' URL in the custom field.

update_post_meta( $post_id,'image_url',$image_url);

share|improve this answer

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.