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.

2 Answers 2


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.

  • 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, 2011 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);

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.