Programming TipsWordPress Tips

WordPress: Create New Post For Each Image Added

Recently we’ve been working on a very image-reliant, meme t-shirt selling website that required some bulk uploading functionality for the thousands of memes they’ve collected. We are using a custom post type in WordPress called “Memes”, but this can work for any post type.

How To Bulk Create Posts When An Image Is Uploaded To The WordPress Media Library

What we’ll do is put a simple WordPress hook into our functions.php file in our theme that will listen for images being uploaded to the WordPress media library, then create an individual post, making the uploaded image become the featured image (post thumbnail) of the post. I’m really impressed with WordPress in this situation — it’s amazing that the solution is so simple:

add_action('add_attachment', 'create_post');
function create_post( $attach_ID ) {

    $attachment = get_post( $attach_ID );

    $my_post_data = array(
                'post_title' => $attachment->post_title,
                'post_type' => 'post',
                'post_category' => array('0'),
                'post_status' => 'publish'
    );
    $post_id = wp_insert_post( $my_post_data );

    // attach media to post
    wp_update_post( array(
        'ID' => $attach_ID,
        'post_parent' => $post_id,
    ) );

    set_post_thumbnail( $post_id, $attach_ID );

    return $attach_ID;
}

Again, you can change the post type specified in the $my_post_data array to be “post”, “page” or any custom post type you wish. After you’ve added this script to your functions.php file, go into WordPress’ media manager and upload as many images as you want. The script works flawlessly.

Be sure to comment the code out after uploading, so future uploads in posts don’t create more posts (unless that’s what you want).

Join the discussion One Comment

  • EvanRedmon says:

    Thanks very much – this was the clean solution I was looking for. Perfect.

    One question: when you said “Be sure to comment the code out after uploading, so future uploads in posts don’t create more posts (unless that’s what you want).” — Is there an edit that would allow posts to be generated selectively? Let’s say I wanted to upload two new images, one that would generate a post and one that would not. Is there any way to allow for that, on an ongoing basis throughout the life of the site?

Leave a Reply

Allen Gingrich

Author Allen Gingrich

Allen has worked on the web for over a dozen years. Like many young entrepreneurs, he began with a small workspace in his basement, where he eventually formed Ideas and Pixels. The rest, well, is history. Allen enjoys fine wines, weight training, and beautiful, semantic code.

More posts by Allen Gingrich

Join the discussion One Comment

  • EvanRedmon says:

    Thanks very much – this was the clean solution I was looking for. Perfect.

    One question: when you said “Be sure to comment the code out after uploading, so future uploads in posts don’t create more posts (unless that’s what you want).” — Is there an edit that would allow posts to be generated selectively? Let’s say I wanted to upload two new images, one that would generate a post and one that would not. Is there any way to allow for that, on an ongoing basis throughout the life of the site?

Leave a Reply