Advertisement
marjwyatt

Autoset Featured Image

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