Try using set_post_thumbnail()
.
Edit by Otto: You clarified your question, so I'll clarify the response Chip gave.
Basically, you need to make the 'attachment' for the post as well. When an image is uploaded into the WordPress media library, a special post entry is made for it with a post type of attachment. This attachment is linked to some specific post via the post_parent identifier.
So if you know the ID of the attachment, then calling set_post_thumbnail with the post object or ID and the attachment ID will simply set the post thumbnail flag.
If you have not created the attachment yet, then you will need to do that first. Easiest way to do that is with wp_insert_attachment()
. This function takes an array of a few parameters, the filename (the file must already be in the proper uploads directory), and the post ID of the parent post that you want to attach the attachment to.
Just having a file uploaded and attached to a post doesn't do anything automatically. This is simply a sort of categorization mechanism. The gallery mechanism, for example, uses the attached images of a post to build the [gallery] for that post. A thumbnail for a post is just one of the attached images which has be set to be the thumbnail.
More info on how to use wp_insert_attachment can be found in the codex (linked above).