I am uploading all my images through media library.

After uploading images I am giving image name in my bulk upload excel file and adding that image name into post and postmeta table.

The image is showing at the front end and in the featured image place also in the admin, but in media library it is showing blank images.

What am I missing?

here is my code while doing excel file bulk upload for inserting image i using this code

$filename = $loopimage;
$title = explode(".",$loopimage);
$wp_filetype = wp_check_filetype($filename, null);

$attachment = array(                                                      
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => $title[0],
    'post_content' => '',
    'post_status' => 'inherit'

$attach_id = wp_insert_attachment($attachment, $filename, $last_id);

if($imgkey == 0) {
    add_post_meta($last_id, '_thumbnail_id', $attach_id);  

I am getting this $loopimage from excel file for each product excel will mention the name of the image like 'subcat3.jpg'.This images is already uploaded through wordpress.

while inserting I will check $loopimage is empty or not if not I will set that image as the featured image of that product which I have inserted. While inserting the product I will get last inserted id that is the post_id of that product I am storing it in $last_id variable

  • 1
    What bulk upload Excel file? How are you uploading it? Are you sure that your bulk upload can actually associate your data with the images? If you are able to be very specific about your data and what you are doing then we might be able to come up with a helpful answer. – Andy Macaulay-Brook Jul 26 '16 at 10:47
  • Where do you define $loopimage and $last_id? – Andy Macaulay-Brook Jul 26 '16 at 11:07
  • $loopimage is nothing but its an image name and $last_id is the last inserted post_id – user1187 Jul 26 '16 at 11:12
  • And where do these values come from? If you can expand your question so that we can see all the relevant code and also understand what the data in your CSV represents then things might be clearer. – Andy Macaulay-Brook Jul 26 '16 at 11:14

Your $loopimage field in your spreadsheet only has the filename and not the file path, but wp_insert_attachment needs the full pathname in the 2nd argument.

Additionally though, if you have uploaded all of these images using the Media Manager interface then they will already be stored as attachments. You'd be better off using the file path and file name to find the existing attachment ID and then setting that as the featured image of your post.

You can add the current upload path to a filename like this:

$upload_dir = wp_upload_dir(); $filename = $upload_dir[path] . '\' . $loopimage; // I see you are on Windows, so I've used a backslash

But if you are organising your uploads by date, the images you want may not be in this folder.

  • I have given the full path like $filename = get_site_url()."/wp-content/uploads/".$loopimage; and still its not working – user1187 Jul 26 '16 at 11:40
  • That's the URL - check my update – Andy Macaulay-Brook Jul 26 '16 at 11:55

Your Answer

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

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