I have over 400 posts with images inside them, I have got a new template which requires a featured image for each post, something my last template did not require… I am wondering if there is a script I can add to my functions.php to be able to grab the first image in each post and set it as the featured… So far i have found this, but it is not working…
function auto_set_featured() {
global $post;
$has_thumb = has_post_thumbnail($post->ID);
if (!$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);
}
}
}
}
add_action('the_post', 'auto_set_featured');
add_action('save_post', 'auto_set_featured');
add_action('draft_to_publish', 'auto_set_featured');
add_action('new_to_publish', 'auto_set_featured');
add_action('pending_to_publish', 'auto_set_featured');
add_action('future_to_publish', 'auto_set_featured');
This script will work for new posts, but i need it to affect all my older posts, any suggestions?
Regarding the code you posted, I would say some things:
'save_post'
is triggered everytime a post is created or updated$post
:'save_post'
will pass the post id, you can use it, in addition, preparing the function to receive an argument will help you to run the same function programmaticallyThe edited version of your code becomes:
Now, the only thing you need for old posts, is to retrieve them with a query and then run the same function for every post.
Just keep attention to perform the task only once: it’s a very time & resource consuming task, so it should be ran only once, possibly on backend.
I’ll use a transient for the purpose:
After adding this code to your
functions.php
or to a plugin, log in the backend, and prepare yourself to wait some seconds before the dashboard appear, but after that all post should have a thumbnail, at least every post that has an image uploaded in it.If everything goes as it should you can the remove the last code snippet keeping only the first.
Note my code require php 5.3+
Use this code in your child themes functions file and them regenerate thumbnails
“Set All First Images As Featured” worked for me.
With a hack it could reverse the operation.
Easy Add Thumbnail may also do the trick, and the pro version of Quick Featured Images does it, and I think I saw another plugin that does this when I searched.
Simple Function Code
I have had to develop the same functionality. On my case, I had to set the featured images according one posts category.
Here you have the code you must insert in a template of your current theme. This template must be called, for example: ‘page-set-images.php’ (if you need it you have more information about how creating pages templates here):
As well, you will have to publish one page with this slug on my example: ‘set-images’. Once the page is published, you must go to that page on your browser and add as parameter ‘setting=1’. So, on my example it would be:
https://domain.com/set-images/?setting=1
.Also, once executed the script, on the page you will be able to see the total of posts of the category, the total of posts of that category without featured image and the total of modified posts.