Advertisement
developerjustin

Untitled

Mar 21st, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. dr_cd_save_thumbnails();
  2.  
  3. function dr_cd_save_thumbnails(){
  4.     $wp_query = query_posts(array(
  5.     'post_type' => 'issue',
  6.     'posts_per_page' => '-1'       
  7.     ));  
  8.  
  9.     // the loop
  10.     if (have_posts()) : while (have_posts()) : the_post();
  11.  
  12.         //Companies
  13.         $daID = get_the_ID();
  14.         $path = get_post_meta($daID, "toc_cover_image", true);
  15.         $fixpath = explode('/',$path);
  16.        
  17.         $datfile = 'http://digitalrelativity.net/wp-content/uploads/'.$fixpath[2].'/'.$fixpath[3].'/'.$fixpath[4];
  18.  
  19.         dr_cd_set_featured_image($daID,$datfile);
  20.         sleep(.5);
  21.         echo $datfile . '<br>';
  22.  
  23.     endwhile;
  24.  
  25.     endif;
  26.  
  27.     wp_reset_query();
  28.    
  29. }
  30.  
  31. function dr_cd_set_featured_image($post_id,$filename) {
  32.     $wp_filetype = wp_check_filetype(basename($filename), null );
  33.     $attachment = array(
  34.         'post_mime_type' => $wp_filetype['type'],
  35.         'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
  36.         'post_content' => '',
  37.         'post_status' => 'inherit'
  38.     );
  39.     $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
  40.     echo $attach_id;
  41.     // you must first include the image.php file
  42.     // for the function wp_generate_attachment_metadata() to work
  43.     require_once($_SERVER['DOCUMENT_ROOT'] . "/wp-admin" . '/includes/image.php');
  44.  
  45.     $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  46.    
  47.     echo '<pre>';
  48.     var_dump( $attach_data);
  49.     echo '</pre>';
  50.    
  51.    
  52.     if (wp_update_attachment_metadata( $attach_id,  $attach_data )) {
  53.         // set as featured image
  54.         return update_post_meta($post_id, '_thumbnail_id', $attach_id);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement