How can I bulk upload images and automatically create posts for each one at the same time?

I know how to upload multiple images into an existing post, but this is a different scenario. This is for a large catalog of assets, with each custom post-type post representing an image asset (thus a single image is assigned as the “featured image” for each post).

The whole system works great for one-at-a-time asset creation, but far too time-consuming. I need to be able to “batch” upload multiple images at once, then have a new post created for each image, then attach the image to the new post as the “featured image”.

Read More

There will be no text content entered for each post – just some metadata and custom taxonomy assignments, so it should be possible to batch-assign the metadata and taxonomy during this batch creation process.

I’ve researched solutions from XML-RPC post creation (which doesn’t usually handle image upload/assignment) to plugins that pull files from a server directory to the media library (which doesn’t cover post creation), and jquery multiple file uploaders (that basically just dump files in a directory).

I am relatively versed in php, having built plugins and themes, but I’m stumped at how to handle this process, as it requires the first step of getting the files to the server in some temporary capacity, then generating posts based on the files uploaded, and assigning some identifying metadata to the whole batch.

I’m hoping this can be done in a custom admin panel, but if I have to do this outside of wp-admin, that’s fine too…

Ideas?

Related posts

Leave a Reply

3 comments

  1. There is this plugin: Automatic Featured Image Posts Plugin

    From the plugin page:

    Automatic Featured Image Posts creates a new post with a Featured Image every time an image is uploaded. Through the plugin settings page, you can set the image to publish and assign itself to one of your other existing custom post types and/or post formats.

    Basically, every image that is uploaded generates a post (of your chosen post-type) and is set as the featured image of that post.

    I installed this on my local machine. The settings page looks like this and allows you to select which post type, including custom post types, you want to assign photo uploads to, and what publish status you want.

    Screenshot of plugin

    To put the plugin into practice – navigate to your chosen post type, open a new post and upload media.

    To bulk upload photos, simply highlight multiple photos in the “upload” dialogue box. I’m highlighting 8 photos here, but I see no reason why it couldn’t be 80 or 800, unless there are limits I don’t know about in the wordpress image uploader.

    enter image description here

    The titles of the posts are set by the image file names. You should be able to work with that and call them in your theme with the_title()

    I tested locally and it’s working in WordPress 3.6.

    I’m sure there is a more robust or flexible way to accomplish this, but in this case, the plugin seems to do exactly what you’re asking, with the exception of assigning meta-data. Maybe someone else could flesh that part out a little bit.

    If you needed dynamically generated post content, you could at least start with the plugin and iterate from there. One thought there would be to use post-formats or page-templates to determine how the posts are displayed.

    Note: Make sure you have all your image_sizes set in functions.php. I’d hate to have to undo / delete 10,000 photos, or run an extremely long “regenerate thumbnails” just because I forgot or changed the image size!

  2. This script is a proof of concept (tested and working), it is not a plugin and is meant to be hacked with, it assumes a few things:

    1. It uses wp_insert_post so it’s advised you do not hook it into any admin hooks, so just run it once!
    2. The images must be in the wp-contentuploads folder, changing this would require more hoops to jump through. The example uses a custom folder called images in the uploads folder, you can change this part.
    3. It does not do any error checking, I only tested it on a folder with 20 images so results might vary:)

    The code below will iterate through the wp-contentuploadsimages folder and create a post title based on the name of the image being attached to it. You probably want to change this to something better or possible enter meta data using other data you have (Exif maybe).

    function WPSE_1595_image_post() {
    
        // We need to use the default uploads dir
        $wp_upload_dir =  wp_upload_dir();
        // The actual folder
        $wp_upload_images = $wp_upload_dir['basedir'] . '/images';
    
        require_once(ABSPATH . 'wp-admin/includes/image.php');
    
        foreach (new DirectoryIterator($wp_upload_images) as $fileInfo) {
    
            if($fileInfo->isDot()) continue;
    
            $image_base = $fileInfo->getFilename();
            $image_name = pathinfo($fileInfo, PATHINFO_FILENAME);
    
            //Customize this post data as you wish
            $my_post_data = array(
                'post_title' => $image_name,
                'post_type' => 'post',
                'post_category' => array('1'),
                'post_author'   => 1,
                'post_status' => 'publish'
            );
    
            // We need the ID for the attachment
            $post_id = wp_insert_post($my_post_data);
    
            $wp_filetype = wp_check_filetype($image_base, null );
    
             //Customize this attachment data as you wish
            $attachment = array(
                 'guid' => $wp_upload_dir['url'] . '/' . $image_name, 
                 'post_mime_type' => $wp_filetype['type'],
                 'post_title' => 'child-' . $image_name,
                 'post_content' => '',
                 'post_status' => 'inherit'
            );
    
            $imagefile = $wp_upload_images . '/' . $image_base;
            $attach_id = wp_insert_attachment( $attachment, $imagefile, $post_id );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $imagefile );
    
            wp_update_attachment_metadata( $attach_id, $attach_data );
        }   
    }
    

    For anything over a few thousand images you would probably have an easier time using: