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.
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 withwp_insert_atachment()
) as thumbnail for the post.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);