miriamdepaula

WordPress: Auto set post thumbnail

Apr 8th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php
  2. function autoset_featured() {
  3.           global $post;
  4.           $already_has_thumb = has_post_thumbnail($post->ID);
  5.               if (!$already_has_thumb)  {
  6.               $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
  7.                           if ($attached_image) {
  8.                                 foreach ($attached_image as $attachment_id => $attachment) {
  9.                                 set_post_thumbnail($post->ID, $attachment_id);
  10.                                 }
  11.                            }
  12.                         }
  13.       }  //end function
  14. add_action('the_post', 'autoset_featured');
  15. add_action('save_post', 'autoset_featured');
  16. add_action('draft_to_publish', 'autoset_featured');
  17. add_action('new_to_publish', 'autoset_featured');
  18. add_action('pending_to_publish', 'autoset_featured');
  19. add_action('future_to_publish', 'autoset_featured');
  20.  
  21. ?>
Add Comment
Please, Sign In to add comment