Last active
December 11, 2015 07:49
-
-
Save thefrosty/4569201 to your computer and use it in GitHub Desktop.
Recently I accidentally set all posts featured image to the most recent uploaded image. Here is a snippet to fix it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', 'ap_reset_featured_images' ); | |
function ap_reset_featured_images() { | |
if ( !is_user_logged_in() ) return; | |
if ( isset( $_GET['reset-featured-image'] ) && true == $_GET['reset-featured-image'] ) { | |
$allposts = get_posts( array( 'posts_per_page' => 9999 ) ); | |
foreach( $allposts as $post ) : setup_postdata( $post ); | |
$attached_image = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => '1' ) ); | |
if ( $attached_image ) { | |
$count = 0; | |
foreach ( $attached_image as $attachment_id => $attachment ) { | |
if ( wp_attachment_is_image( $attachment->ID ) ) $count++; | |
if ( $count >= 2 ) break; | |
set_post_thumbnail( $post->ID, $attachment_id ); | |
} | |
} | |
endforeach; | |
wp_die( __( 'All post images updated.' ), __( 'Complete' ), array( 'response' => 201 ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment