1

I'm trying to migrate a site to WP. We have a whole bunch of pictures, and I have done some research and figured -- I think -- my best bet is a CSV import of the data, storing the relative URL of the image as a custom field.

So, I found this --which looks ideal -- and I implemented it thus (in functions.php with a global call added as I was getting a 'no object' error):

function store_cf_featured_image() {
global $post,$wpdb;

$uploads = wp_upload_dir();

// Get all attachment IDs and filenames
$results = $wpdb->get_results("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file'");

// Create an 'index' of attachment IDs and their filenames
$attachments = array();
foreach ($results as $row)
    $attachments[ intval($row->post_id) ] = $row->meta_value;

// Get all featured images
$images = $wpdb->get_results("SELECT post_id, meta_value AS 'url' FROM $wpdb->postmeta WHERE meta_key = 'featured_image'");

// Loop over each image and try and find attachment post
foreach ($images as $image) {
    if (preg_match('#^https?://#', $image->url))
        $image->url = str_replace($uploads['baseurl'], '', $image->url); // get relative URL if absolute

    $filename = ltrim($image->url, '/');

    if ($attachment_ID = array_search($filename, $attachments)) {
        // found attachment, set post thumbnail and delete featured image
        update_post_meta($image->post_id, '_thumbnail_id', $attachment_ID);
        delete_post_meta($image->post_ID, 'featured_image');
    }


}


}

add_action( 'init', 'store_cf_featured_image' );

However, when I put a link in my custom field 'featured_image', and save it... nothing. No errors, but no nothing. I update the post and come back to it, but the link is still just a link the the custom field and no 'featured image' has been set.

Does anyone have any ideas what the issue might be? an am I right in thinking this will store the image as part of built in 'Media Library', now?

AlsoAlso: Will I be able to adapt this code to pull in Alt text etc for each image from custom fields.

Any input greatly appreciated! Thanks.

4
  • Is the code hooked to something? when does it run?
    – Mario
    Commented Jul 28, 2011 at 13:29
  • Aha, I don't know. I'm still pretty new to WP and PHP in general. I thought add_action( 'init', 'store_cf_featured_image' ); would run it? Maybe it isn't running... How do I make sure? (i assume it is runnign because it was throwing up errors before i put in 'globa $past,$wpdb')
    – josh
    Commented Jul 28, 2011 at 13:49
  • Right sorry, didn't see the add_action.
    – Mario
    Commented Jul 28, 2011 at 13:55
  • No worries! So as far as you can see the code looks like it should work? I mean... no glaring errors?
    – josh
    Commented Jul 28, 2011 at 14:23

1 Answer 1

3

so you're images are already in a custom field?

you could use the Get the Image plugin.... i know it will pull your thumbnail from a custom field and i think it might automatically set it as the featured image, tho it has been a while and i'm not sure of that.
http://wordpress.org/extend/plugins/get-the-image/

4
  • I had a look at Get the Image, it let's you pull images from custom fields. But it doesn't store them or set them as featured (as far as I can see). Ideally I'd like to import all the main images for each post, and attach them using the normal WP media library, then i can get them resized etc automagically as I need and they are available to most plugins
    – josh
    Commented Jul 28, 2011 at 13:51
  • ambitious. i thought GTI set your image as the featured image, but maybe that is for the ones it 'finds' in the post content? i can't remember. Commented Jul 28, 2011 at 19:52
  • Aha. Okay. I've read around this a bit more, and Get the Image doesn't set the 'featured image' but you can use it to make a featured image in an alternate manner! Which, depending on your view of how many DB queries you want to have etc... might be a better way of doing things.
    – josh
    Commented Jul 29, 2011 at 17:30
  • Either way, this should do what I need at the moment, so many thanks for the responses. If I figure out how to combine Get the Image with WP auto-image resizing for thumbnails etc I'll post back on here! Thanks @helgatheviking!
    – josh
    Commented Jul 29, 2011 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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