wordpress - Upload images to media library based on custom field -


i have migrated custom built site. not totally sure doing, created custom field called featured_image , put url image in each post.

now.

is there way, can loop through of posts , generate thumbnail / featured image url in custom field?

hope makes sense!

more info.

i have moved 7000 posts, each post has custom field has single url image in it. take urls , make them featured image associated posts.

i have plugin can take first image in post , republish, may not practical 7000!

thanks

ok, found answer hacking apart plugin.

first, loop through postmeta table

$postid_list = $wpdb->get_results("select distinct post_id yars_postmeta meta_key='featured_image' order post_id desc limit 10");         if (!$postid_list){                 die('no posts images found.');         }         foreach ($postid_list $v) {             $post_id = $v->post_id;             //$options['url_method'] = $url_method;             echo fig_fetch_images($post_id).'<br/>';         } 

then in function image , upload media library set featured image post id

function fig_fetch_images( $post_id ) {  global $wpdb; //check make sure function not executed more once on save  if ( defined('doing_autosave') && doing_autosave )  return;  if ( !current_user_can('edit_post', $post_id) )  return;  remove_action('publish_post', 'fetch_images');    //$post = get_post($post_id);    $first_image = ''; $key = 'featured_image'; $first_image = get_post_meta($post_id, $key, true); $wpdb->query("update yars_postmeta set meta_key ='featured_image_uploaded'where meta_key='featured_image' , post_id=".$post_id);  if (strpos($first_image,$_server['http_host'])===false) {      //fetch , store image       $get = wp_remote_get( $first_image );      $type = wp_remote_retrieve_header( $get, 'content-type' );      $mirror = wp_upload_bits(rawurldecode(basename( $first_image )), '', wp_remote_retrieve_body( $get ) );       //attachment options      $attachment = array(      'post_title'=> basename( $first_image ),      'post_mime_type' => $type      );      // add image media library , set featured image      $attach_id = wp_insert_attachment( $attachment, $mirror['file'], $post_id );      $attach_data = wp_generate_attachment_metadata( $attach_id, $first_image );      wp_update_attachment_metadata( $attach_id, $attach_data );      set_post_thumbnail( $post_id, $attach_id );       // re-hook function      add_action('publish_post', 'fetch_images');       } return ('done post '. $post_id .' : '. $first_image); 

}

the original plugin hotlink image cacher!


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -