WordPress.org

Ready to get started?Download WordPress

Forums

CSV Importer
Featured Image (21 posts)

  1. nagrap
    Member
    Posted 2 years ago #

    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/

  2. dompl
    Member
    Posted 2 years ago #

    Hi nagrap, did you gave any luck with that?

  3. nagrap
    Member
    Posted 2 years ago #

    I still have not managed to get it to work

  4. grex22
    Member
    Posted 2 years ago #

    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...

  5. Joy
    Member
    Posted 2 years ago #

    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.

  6. nagrap
    Member
    Posted 2 years ago #

    Joy

    Can you let me know which file this function is contained within as I have searched but cannot find.

    Thanks in advance...

  7. Joy
    Member
    Posted 2 years ago #

    It is in csv-importer/csv_importer.php

  8. nagrap
    Member
    Posted 2 years ago #

    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>
  9. Joy
    Member
    Posted 2 years ago #

    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.

  10. emerge_
    Member
    Posted 2 years ago #

    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 :(

  11. Joy
    Member
    Posted 2 years ago #

    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.

  12. dracula385
    Member
    Posted 2 years ago #

    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
  13. Louis_Dea
    Member
    Posted 2 years ago #

    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!

  14. indianplayschools
    Member
    Posted 2 years ago #

    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.

  15. Syndicate Software
    Member
    Posted 2 years ago #

    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!

  16. MCM
    Member
    Posted 1 year ago #

    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 etc.

  17. my-web
    Member
    Posted 1 year ago #

    omg wthat the way for import featured image through CSV?

  18. my-web
    Member
    Posted 1 year ago #

    omg just now i know...thanks to @Joy and @Syndicate Software

  19. steve2425
    Member
    Posted 1 year ago #

    asking the same question as nathmie,
    is it possible to import single or multiple images through URL?

    http://www.website.com/images/listings/image1.gif,http://www.website.com/images/listings/image2.gif

  20. bheadrick
    Member
    Posted 1 year ago #

    I just tweaked the plugin to allow attachments of any kind to be downloaded and imported into the media library. This is assuming the file(s) are already online somewhere (but not in your media library folder). All you need to do is have the full url of the image/document/whatever in the field with the heading being csv_attachment_whatever (with "whatever" being the title of the attachment. Also, if the column is csv_attachment_thumbnail, then it sets that attachment as the featured image of the post.

    let me know if anyone is interested. I forked it on Github

    Still working on getting functionality going where it will find an attachment you've already uploaded by the filename.

  21. steve2425
    Member
    Posted 1 year ago #

    thats great. i am checking out the forked plugins. thanks

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic