how to attach an image to a custom post type in wordpress using just code

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.

Related posts

Leave a Reply

2 comments

  1. 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);