I found this PHP code that will set the first attached image as the post thumbnail. https://wordpress.stackexchange.com/a/23768
Is it possible to loop through old posts that have attachments using WP_Query() to set the post thumbnail for old posts? If the post has more than 1 attachement, I’d like to set the first attachment as the post thumbnail.
I’d like to do this only once to get everything up to date, then I can incorporate the PHP code from the answer above to modify new posts going forward.
As requested, here is the code from the aforementioned linked answer.
function myprefix_main_image() {
$attachments = get_children( 'post_parent='.$post->ID.'&post_type=attachment&post_mime_type=image&order=desc' );
if( $attachments ) {
$keys = array_reverse( $attachments );
set_post_thumbnail( $post->ID, $keys[0]->ID );
};
}
The first step is to loop through all the posts:
Now, that will get all the posts; but we’re only interested in posts that don’t have a featured image set. WordPress stores the featured image via a post custom meta field:
_thumbnail_id
; so, we can use that in a meta query, to get only the posts that don’t have a set featured image:Now, set up the loop:
Now, query for attachments:
Now wrap the whole thing inside a function, so you can call it somewhere, or hook it into something:
You can improve this function, by stepping through the posts in batches, or by defining your own post custom meta to query against, so that you only process each post once, etc.