• Resolved nagrap

    (@nagrap)


    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…

    https://www.ads-software.com/extend/plugins/csv-importer/

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

    (@dompl)

    Hi nagrap, did you gave any luck with that?

    Thread Starter nagrap

    (@nagrap)

    I still have not managed to get it to work

    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…

    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

    (@nagrap)

    Joy

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

    Thanks in advance…

    It is in csv-importer/csv_importer.php

    Thread Starter nagrap

    (@nagrap)

    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:[email protected]?subject=CSV Importer Feeback ~">Send Feedback</a></td>
      </tr>
    </table>
    
    </body>
    </html>

    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.

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

    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.

    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

    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!

    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.

    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!

    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?

    https://www.website.com/images/listings/image1.gif etc.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘[Plugin: CSV Importer] Featured Image’ is closed to new replies.