Title: [Plugin: CSV Importer] Featured Image
Last modified: August 20, 2016

---

# [Plugin: CSV Importer] Featured Image

 *  Resolved [nagrap](https://wordpress.org/support/users/nagrap/)
 * (@nagrap)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/)
 * Hi
 * I am trying to set the Featured Image upon import. I have tried to follow another
   post on the internet but was not able to get to work, can someone please help?
 * I have uploaded an image to the media gallery. I have then added a column to 
   the import spreadsheet labelled image_thumb and posted the full URL to the image.
   Upon import the system creates a custom field called image_thumb. What I want
   it to do is set the image as the featured image.
 * Someone please help… should I be using a different name for the image column?
 * Thanks in advance…
 * [http://wordpress.org/extend/plugins/csv-importer/](http://wordpress.org/extend/plugins/csv-importer/)

Viewing 15 replies - 1 through 15 (of 20 total)

1 [2](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/page/2/?output_format=md)

 *  [dompl](https://wordpress.org/support/users/dompl/)
 * (@dompl)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411077)
 * Hi nagrap, did you gave any luck with that?
 *  Thread Starter [nagrap](https://wordpress.org/support/users/nagrap/)
 * (@nagrap)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411078)
 * I still have not managed to get it to work
 *  [grex22](https://wordpress.org/support/users/grex22/)
 * (@grex22)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411136)
 * My sense is that this may be trickier to do than just giving the post an image
   URL. The featured image field for a post actually references an Attachment ID.(
   attachments are basically a post type that is created every time you upload a
   file.)
 * I’m sure there’d be SOME way to do this, but I have no clue. I’m currently trying
   to figure something out like this myself…
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411140)
 * I have done this awhile ago.
    I loaded all of my images first (I actually used
   Import Legacy Media). Then in the row for a post, I set the column called _wp_attached_file
   to the filename of the image (not URL but filename). I also use a column called
   _wp_attachment_image_alt so the image has keywords in the alt tag. Then I modified
   the function create_custom_fields to look like below:
 *     ```
       function create_custom_fields($post_id, $data) {
           global $wpdb;
           $many=0;
           foreach ($data as $k => $v) {
               // anything that doesn't start with csv_ is a custom field
               if (!preg_match('/^csv_/', $k) && $v != '') {
                 if ($k=='_wp_attached_file') {
                   $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value = '$v'");
                   $wpdb->query( "UPDATE $wpdb->posts SET post_parent = '$post_id' WHERE ID = $imageID" );
                   add_post_meta($imageID, '_wp_attachment_image_alt', trim($data['csv_post_title']).' '.trim($data['csv_post_excerpt']));
                   if (!$many++) add_post_meta($post_id, '_thumbnail_id', $imageID);     //makes it featured image
                 }
                 else
                   add_post_meta($post_id, $k, $v);
               }
           }
       }
       ```
   
 * This allows me to attach multiple images and only the first one is set as the
   featured image.
 *  Thread Starter [nagrap](https://wordpress.org/support/users/nagrap/)
 * (@nagrap)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411142)
 * Joy
 * Can you let me know which file this function is contained within as I have searched
   but cannot find.
 * Thanks in advance…
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411143)
 * It is in csv-importer/csv_importer.php
 *  Thread Starter [nagrap](https://wordpress.org/support/users/nagrap/)
 * (@nagrap)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411144)
 * Joy,
 * I don’t see any function in csv_importer.php named create_custom_fields (see 
   below)
 * Should I be adding the function as a new function in there? How should I then
   call the function?
 *     ```
       <?
   
       // Include required files
       include("includes/php/globals.php");
       include("includes/php/functions.php");
   
       CreateForm("index.php", $stage);
   
       ?>
   
       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   
       <html>
       <head>
       	<title>PHP CSV Importer: v<?=$scriptVersion?> by <?=$authorName?></title>
       	<link rel="stylesheet" href="includes/css/csv_importer.css" type="text/css">
       	<script>
       		<?=$js_block?>
       	</script>
       	<?=$js_scripts?>
       </head>
   
       <body onLoad="<?=$bodyOnLoad?>">
       <table width="780" border="0" cellspacing="1" cellpadding="2" align="center">
         <tr class="title">
           <td>PHP CSV Importer</td>
         </tr>
         <tr>
           <td>
           		<table border="0" cellspacing="0" cellpadding="2" width="<? echo ($tableWidth) ? $tableWidth : "640" ?>" align="center">
       			<tr class="instructions">
       				<td><?= $instructions?></td>
       			</tr>
       			<tr class="spacer">
       				<td> </td>
       			</tr>
       			<form name="form1" action="<?=$PHP_SELF?>" method="POST" onSubmit="return(ValidateForm(this<?=$validateFormArgs?>))">
       				<tr>
       					<td>
       						<table cellspacing=1 cellpadding=0 border=0 width=100%>
       							<?=$display_block?>
       						</table>
       					</td>
       				<tr>
       					<td> </td>
       				</tr>
       				<tr class="submit">
       					<td><input type="submit" name="mainSubmit" value="<? echo ($submitValue) ? $submitValue : "Next »"?>"></td>
       				</tr>
       				<input type="hidden" name="stage" value="<?=$stage?>">
       			</form>
       		</table>
       	</td>
         </tr>
         <tr>
           <td> </td>
         </tr>
         <tr class="footer">
           <td>Author: <?=$authorName?> | Version: <?=$scriptVersion?> | <a href="mailto:sir_tripod@hotmail.com?subject=CSV Importer Feeback ~">Send Feedback</a></td>
         </tr>
       </table>
   
       </body>
       </html>
       ```
   
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411145)
 * Apparently you are looking at a different plugin called PHP CSV Importer. The
   one I am talking about is CSV Importer and it is found at the link in the first
   post in this thread.
 *  [emerge_](https://wordpress.org/support/users/emerge_/)
 * (@emerge_)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411146)
 * **Joy**, I’ve tried to do it, but it’s not working.
 * Uploaded images to media library, tried to import CSV but it’s not adding featured
   images to posts.
 * _wp_attached_file – should be for example ‘1.jpg’ or ‘1’?
    I’ve tried both but
   nothing works 🙁
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411148)
 * emerge_,
    I can’t debug your system, sorry… The filename should be the complete
   name, for example ‘1.jpg’ not ‘1’. It is case sensitive.
 *  [drax](https://wordpress.org/support/users/dracula385/)
 * (@dracula385)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411153)
 * i’ve managed to get it working….
 *     ```
       function create_custom_fields($post_id, $data) {
           global $wpdb;
           foreach ($data as $k => $v) {
               // anything that doesn't start with csv_ is a custom field
               if (!preg_match('/^csv_/', $k) && $v != '') {
                 if ($k=='_wp_attached_file') {
                   $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value LIKE '%$v%'");
                   $wpdb->query( "UPDATE $wpdb->posts SET post_parent = '$post_id' WHERE ID = $imageID" );
                   add_post_meta($imageID, '_wp_attachment_image_alt', trim($data['csv_post_title']).' '.trim($data['csv_post_excerpt']));
                   add_post_meta($post_id, '_thumbnail_id', $imageID);
                 }
                 else
                   add_post_meta($post_id, $k, $v);
               }
           }
       }
       ```
   
 * Problem was that the when i upload images trough Import Legacy Media images went
   to some folder so query didn’t find any image id.
 * Note that this version is just when you upload one image to set it featured..
 * CSV looks like this:
 *     ```
       csv_post_title,csv_post_categories,_wp_attached_file,_wp_attachment_image_alt
       Specijalni program radne i zaštitne uniforme 2,Specijalno,specijalno2.jpg,specijalno2
       Specijalni program radne i zašitne uniforme 3,Specijalno,specijalno3.jpg,specijalno3
       Specijalni program radne i zašitne uniforme 4,Specijalno,specijalno4.jpg,specijalno4
       ```
   
 *  [Louis-Philippe Dea](https://wordpress.org/support/users/louis_dea/)
 * (@louis_dea)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411159)
 * I had a situation where I needed to have all the same featured images for the
   posts that I was importing.
 * What I did is upload the image that I need with the WordPress media uploader.
   Then, I just copied the ID of the image. (In my situation, it was ID 17062)
 * In my CSV file, instead of making a “_wp_attached_file” column, I made a “_thumbnail_id”
   column.
 * I copied the function posted here by “Joy” and replaced it with this :
 *     ```
       function create_custom_fields($post_id, $data) {
          	    global $wpdb;
           	$many=0;
       	    foreach ($data as $k => $v) {
       	        // anything that doesn't start with csv_ is a custom field
       	        if (!preg_match('/^csv_/', $k) && $v != '') {
       	          if ($k=='_thumbnail_id') {
       	            $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value = '$v'");
       	            $wpdb->query( "UPDATE $wpdb->posts SET post_parent = '$post_id' WHERE ID = $imageID" );
       	            add_post_meta(trim($data['csv_post_title']).' '.trim($data['csv_post_excerpt']));
       	            if (!$many++) add_post_meta($post_id, '_thumbnail_id', '17062');     //you need to change this ID with yours
       	          }
       	          else
       	            add_post_meta($post_id, $k, $v);
       	        }
       	    }
       	}
       ```
   
 * I’m not a programer so this function only worked for my specific case. Hope it
   will help you too!
 *  [indianplayschools](https://wordpress.org/support/users/indianplayschools/)
 * (@indianplayschools)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411165)
 * I have found an easy solution to this. Just add custom field in csv file and 
   give the URL of the image (should be preloaded to media. It is being displaying
   for me when I use Types and Views Plugin.
 *  [Syndicate Software](https://wordpress.org/support/users/syndicate-software/)
 * (@syndicate-software)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411172)
 * I used Joy’s code from above, but it wasn’t working at first. I didn’t use the
   Import Legacy Media that she mentioned though – that might have been the problem.
   I used WP’s (ver 3.4.1) built in media uploader.
 * After a little inspection of the database, I found out what was going on – the
   WP uploader was adding the year & month directory names to the name of the image
   in the wp_postmeta table. (instead of the expected “image.jpg” it was “2012/8/
   image.jpg”)
 * Once I realized this, a quick change of the SQL query made it work perfectly.
   
   Change this:
 *     ```
       $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value = '$v'");
       ```
   
 * to this:
 *     ```
       $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value LIKE '%$v%'");
       ```
   
 * Don’t forget the double %’s around the $v. Now the query will search for any 
   value that CONTAINS your filename instead of EQUALS your filename.
 * Hope this helps!
 *  [MCM](https://wordpress.org/support/users/nathmie/)
 * (@nathmie)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/#post-2411182)
 * Is there a way to get this all to work by just pointing to a URL in a csv column,
   of the image I do not want to use WP media library but rather store the files
   in a folder?
 * [http://www.website.com/images/listings/image1.gif](http://www.website.com/images/listings/image1.gif)
   etc.

Viewing 15 replies - 1 through 15 (of 20 total)

1 [2](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/page/2/?output_format=md)

The topic ‘[Plugin: CSV Importer] Featured Image’ is closed to new replies.

 * ![](https://ps.w.org/csv-importer/assets/icon-256x256.png?rev=3039593)
 * [CSV Importer](https://wordpress.org/plugins/csv-importer/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/csv-importer/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/csv-importer/)
 * [Active Topics](https://wordpress.org/support/plugin/csv-importer/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/csv-importer/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/csv-importer/reviews/)

 * 20 replies
 * 13 participants
 * Last reply from: [steve2425](https://wordpress.org/support/users/steve2425/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-csv-importer-featured-image/page/2/#post-2411189)
 * Status: resolved