Creating a metabox to upload multiple images

That depends entirely on what you mean by “attach.”

Each WordPress post can already have multiple media attachments – photos, documents, etc. You upload these using the built-in uploader and they’ll all be marked as “attached” to that specific post ID.

You can refer to these later programatically elsewhere. For example, the following code will list out all attachments for a specific post (code from Snipplr):

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}

All of this functionality is accessible via the default “Add Media” button to the far right of “Upload/Insert” on the new post screen. After you add one image, you can click “Select Files” again and upload a second image. Then a third. Then a fourth. As many as you want.

Each of these images will be “attached” to the post … even if they’re not inserted into the content.

Leave a Comment