While looking through the function reference entry for wp_insert_post(), I noticed that there’s no parameter in the array it requires which will allow me to set the ‘Featured Image’ for a post, displayed as the post thumbnail in my theme.
I have looked into functions like set_post_thumbnail(), as suggested by Mr. Bennett, but this seems to be a relatively new addition to WordPress itself and the WordPress codex. As such, there aren’t any sources that I can find which explain how the $thumbnail_id parameter should be acquired and supplied. If this really is the function to use, in what way could I provide it with a valid $thumbnail_id parameter when all I have is an image URL?
Thanks in advance!
You can set an image as post thumbnail when it is in your media library. To add an image in your media library you need to upload it to your server. WordPress already has a function for putting images in your media library, you only need a script that uploads your file.
Usage:
Function:
http://codex.wordpress.org/Function_Reference/wp_upload_dir
http://codex.wordpress.org/Function_Reference/wp_insert_attachment
EDIT:
Added path creation
http://codex.wordpress.org/Function_Reference/wp_mkdir_p
I’d like to improve Robs answer by utilizing the WP core functions
download_url
andmedia_handle_sideload
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).
Just found this and made it much simpler, works but I’m no security scrubber
simple or what? after getting the right files, wordpress will handle the media and upload it, then set it as a thumbnail.
set_post_thumbnail()
is the best function for this requirement.I think, you find the ID of an attachment via
get_children()
orget_posts()
. The result have an array and inside this array is the ID. The follow example for testing; i hope it works; write without tests, only on scratch.For your requirement it is important, that you change
get_the_ID()
with yourpost-ID
; return the ID of the Attachment and this can you use fothset_post_thumbnail()
.