I am trying to do something like this. For multiple file upload, I want to create a post for each image and attach that image with the post. Everything is working. As you can see in the picture image is uploaded and attached to the post that I can see from the media library. But it is not showing either in the post edit page or in the front page with the wp loop. Please help, I would be really grateful.

global $post;
$e_post_id = 0;
if (isset($_POST['groomer_submit_step_one'])) {
$new_post = array(
    'ID'            => $_POST['member_id'],
    'post_type'    => 'groomers',

$post_id = wp_update_post($new_post);

$seting_rating = $_POST['prev_rating']."+".$_POST['rating'];
$seting_comment = $_POST['rating_comments']."+".$_POST['comment'];
update_post_meta($post_id, 'mpa_rating', $seting_rating);
update_post_meta($post_id, 'mpa_rating_comments', $seting_comment);

$files = $_FILES['file_document_up'];
foreach ($files['name'] as $key => $value) {

    $new_post = array(
      'post_title'    => "Rating Pic",
      'post_status'   => 'publish',
      'post_type'     => 'pet_image'

    $post_id = wp_insert_post($new_post);

    add_post_meta($post_id, 'mpa_user_id', $_POST['emp_co']);
    add_post_meta($post_id, 'mpa_g_before', 'c');

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    if ($files['name'][$key]) {
        $file = array(
            'name' => $files['name'][$key],
            'type' => $files['type'][$key],
            'tmp_name' => $files['tmp_name'][$key],
            'error' => $files['error'][$key],
            'size' => $files['size'][$key]
        $_FILES = array("upload_file" => $file);
        $attachment_id = media_handle_upload("upload_file", $post_id);
        set_post_thumbnail($post_id, $attahment_id);

Results in Media Library Page

enter image description here

Results in Post Edit Page

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.