0

I have tens of thousands of images. I also have tens of thousands of articles. These were both imported from a custom CMS.

The image filenames are the IDs they had in the old CMS e.g. 12345.jpg

The articles have a custom field called mainpictureid which contains the ID of an image.

My question is how to use this custom field to set the featured image of a post. I have the basic loop but not sure what the easiest way is to get the thumbnail ID

$children = get_posts('post_type=post');
foreach ($children as $child) {
    $origParentID = get_post_meta($child->ID, 'mainpictureid', true);

    // Somehow get the ID of the image using the ID which will be the filename
    // $thumbID = ???

    set_post_thumbnail( $child->ID, $thumbID); 
}
  • Is the "mainpictureid" a full URL or just the filename? Are all the old CMS images been placed in the "wp-content/uploads" folder? – WisdmLabs Aug 20 '15 at 12:59
  • Just the ID - all uploaded on same day so I know the path. In fact, I've sorted it! – pee2pee Aug 20 '15 at 14:50
0
global $wpdb;
$children = get_posts('post_type=event&numberposts=1');
foreach ($children as $child) {
    $origParentID = get_post_meta($child->ID, 'mainpictureid', true);
    $parsed_url  = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), 'http://localhost/wp-content/uploads/2015/08/'.$origParentID.'.jpg' );
    $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );
    set_post_thumbnail( $child->ID, $attachment[0]); 
}

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.