I have a specific need to import many post with featured images into a custom post type. The issue is, I need this to be a script because it runs off of another system that collects all the data. I currently have the custom post types setup and I have the data to be displayed in the custom post type using custom templates to pull and display the data (lists and profile pages).

I would like to be able to add posts manually (I don't see this being difficult) and add a featured thumbnail using WP built in system. I cannot seem to find any data on how to call the image script directly and pass in the images for it to process and return the data I need for the insertion.

Is this possible? Or do I need to just do this all by hand and just add custom fields to each post to have the two thumbnails I need for the pages? I would rather have this go into the database without the custom fields if possible.

Thanks for any light you can shine on this.

  • there are image resize scripts that you can use. A lot of people use tim thumb to resize images. Then you call the featured image using the script instead of the standard WordPress featured image code. The script would resize the image to whatever image size you needed. Is this what you are looking for?
    – Jamie
    Commented Oct 2, 2012 at 20:05
  • I'm concerned about timThumb since the latest security concern but I do believe it is patched now... I am trying to run a script that can be toggled via the admin panel to update the custom post types. This script would fetch data from a different system (not wordpress) and then insert all the data into the custom post type. I also need it to resize and attach the image to the post. I am still looking for a solution. I really just need to see how to resize and save the images and save to post. I may just need to look at the core to get a concept and go from there. Thanks for your reply
    – Jeremy
    Commented Oct 2, 2012 at 20:14

2 Answers 2


use wp_handle_upload to upload the file, wp_insert_attachment to attach it to a post, and add post meta to the post under the key _thumbnail_id to make that attachment a featured image.


It appears it is possible and not too difficult. Here is the code snippet I used. I also added in some checks to prevent duplicated images in the media manager but that code snippet is unique to my needs (I just used a meta key to each attachment and checked for duplicate keys before adding new attachments.)

I hope this is helpful to some.

I was using a URL for the file. It's easy to change this to work off a locale file.

Save Image to Media Manager

 * Saves image to the media manager
 * @param int $post_id
 * @param string $file
 * @param string $desc
 * @return int false on error 
function addFileToMediaManager($post_id, $file, $desc = '') {
    // Download file to temp location
    $tmp = download_url($file);

    // Set variables for storage
    // fix file filename for query strings
    $file_array = array();
    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;

    // If error storing temporarily, unlink
    if (is_wp_error($tmp)) {
        $file_array['tmp_name'] = '';

    // do the validation and storage stuff
    $attached_id = media_handle_sideload($file_array, $post_id, $desc);
    // If error storing permanently, unlink
    if (is_wp_error($attached_id)) {
        wp_delete_post($post_id, true);
        return false;

    return $attached_id;

Once you have the attachement ID and the post ID just call:

set_post_thumbnail($post_id, $attached_id);

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.