10

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".

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?

5
  • Did you ever find an answer to this? This would be perfect for me...
    – user15057
    Commented Apr 10, 2012 at 22:08
  • I did... but the scenario evolved into an entire framework plugin for the project. It's not a particular short answer - I'd have to document the flow of multiple functions and hooks that all together accomplish this.
    – somatic
    Commented May 2, 2012 at 7:45
  • 1
    In summary, I actually made a two-step process, as for editorial reasons it didn't make sense to actually spawn posts for everything a user uploaded. Instead, the user uploads all the images to a single "batch" post as attachments to that single post. Then, the batch is reviewed and each attachment is accepted/rejected - and once that "batch" post is actually published, a custom save routine takes each accepted attachment and uses wp_insert_post() to create a post with just that single attachment as its featured image.
    – somatic
    Commented May 2, 2012 at 7:47
  • Also A Duplicate: wordpress.stackexchange.com/questions/55616/… Commented Aug 11, 2013 at 9:47
  • How are you adding the meta data, how are images related to the data?
    – Wyck
    Commented Aug 11, 2013 at 13:22

3 Answers 3

5
+100

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!

1
  • you should comment when you downvote - it would be much more helpful to improving the quality of the site.
    – timshutes
    Commented Aug 24, 2013 at 21:37
5

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-content\uploads 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-content\uploads\images 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:

1
  • It keeps adding resized images back to upload/images folder and for that also, it generates new post. The process became recursive so before I removed the function, nearly 800 posts were created. New sizes generated should be done in regular wp media location.
    – Heena Shah
    Commented Aug 20, 2017 at 7:42
1

The "Cleanup uploads folder, Media Library db structure" Question has some plugin suggestions that might be related - albeit not exactly that doing what you ask for. But maybe the information is useful.

Not the answer you're looking for? Browse other questions tagged or ask your own question.