parkeast

Automatically Set Featured Image

Aug 12th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. function auto_featured_image() {
  2.     global $post;
  3.  
  4.     if (!has_post_thumbnail($post->ID)) {
  5.         $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
  6.        
  7.       if ($attached_image) {
  8.               foreach ($attached_image as $attachment_id => $attachment) {
  9.                    set_post_thumbnail($post->ID, $attachment_id);
  10.               }
  11.          }
  12.     }
  13. }
  14. // Use it temporary to generate all featured images
  15. add_action('the_post', 'auto_featured_image');
  16. // Used for new posts
  17. add_action('save_post', 'auto_featured_image');
  18. add_action('draft_to_publish', 'auto_featured_image');
  19. add_action('new_to_publish', 'auto_featured_image');
  20. add_action('pending_to_publish', 'auto_featured_image');
  21. add_action('future_to_publish', 'auto_featured_image');
Advertisement
Add Comment
Please, Sign In to add comment