Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Created April 23, 2013 00:37
Show Gist options
  • Save joshuafredrickson/5439867 to your computer and use it in GitHub Desktop.
Save joshuafredrickson/5439867 to your computer and use it in GitHub Desktop.
WordPress - Genesis: Automatically set featured post images if missing
// Automatically set featured post images if missing
function op_auto_feature_img() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
} else {
set_post_thumbnail($post->ID, '3559'); // Default image
}
}
}
add_action('the_post', 'op_auto_feature_img');
add_action('save_post', 'op_auto_feature_img');
add_action('draft_to_publish', 'op_auto_feature_img');
add_action('new_to_publish', 'op_auto_feature_img');
add_action('pending_to_publish', 'op_auto_feature_img');
add_action('future_to_publish', 'op_auto_feature_img');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment