Are you tired of manually setting featured images for every post? You can use a combination of WordPress hooks and custom functions to set a default featured image for posts or pages that does not have any image attached to it.
Setting a default featured image saves you a lot of time and effort. This is particularly useful when you have a website with a lot of content, and it’s not practical to add featured images manually.
Here’s a step-by-step guide on how to do this:
Step 1: Choose a Default Featured Image
First, make sure you have a default featured image that you want to use. Upload this image to your WordPress Media Library, note its URL, and its attachment ID.
Step 2: Open Your Theme’s functions.php File
In your WordPress theme’s directory, open the functions.php file for editing. You can do this using a code editor or via the WordPress dashboard by going to “Appearance” > “Theme Editor.”
Step 3: Add Custom Function
Add the following code to your functions.php file to define a custom function that sets the default featured image:
function codecraftwp_default_featured_image() {
// Check if the post doesn't have a featured image
if (!has_post_thumbnail()) {
// Replace 'your_default_image_url' with the URL of your default featured image
$default_image_url = 'your_default_image_url';
// Get the attachment ID of the default image
$default_image_id = attachment_url_to_postid($default_image_url);
// Set the default image as the featured image
set_post_thumbnail(get_the_ID(), $default_image_id);
}
}
add_action('save_post', 'codecraftwp_default_featured_image');
Replace ‘your_default_image_url‘ with the actual URL of your default featured image.
After adding the code, save the functions.php file.
Create a new post without setting a featured image. When you publish or update the post, the custom function will check if a featured image is set. If not, it will automatically set the default featured image you specified.
Please note that this code will only set the default featured image for new posts or for existing posts that are updated.
If you want to set the default featured image for all existing posts that don’t have one, you can modify the code to loop through all posts and apply the default image.
Here’s how you can do it:
Set default featured image for all existing posts and pages
function set_default_featured_image_for_existing_posts() {
// Replace 'your_default_image_url' with the URL of your default featured image
$default_image_url = 'your_default_image_url';
// Get the attachment ID of the default image
$default_image_id = attachment_url_to_postid($default_image_url);
// Query to retrieve posts and pages that do not have a featured image set
$args = array(
'post_type' => array('post', 'page'), // You can include additional post types if needed
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS',
),
),
'posts_per_page' => -1, // Retrieve all posts/pages
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
set_post_thumbnail(get_the_ID(), $default_image_id);
}
}
wp_reset_postdata();
}
add_action('init', 'set_default_featured_image_for_existing_posts');
This code will set the default featured image for all existing posts and pages that do not already have a featured image. Just replace ‘your_default_image_url’ with the URL of your default featured image.
Here’s a breakdown of the changes in this code:
- We use the
WP_Queryclass to query for posts and pages that do not have a featured image set. This is done by checking if the_thumbnail_idcustom field (which stores the featured image ID) does not exist. - We loop through the query results and use
set_post_thumbnailto set the default image as the featured image for each post or page. - The action hook
initis used to ensure that this code is executed when WordPress initializes.
Make sure to add this code to your theme’s functions.php file and test it to ensure that it sets the default featured image for existing posts and pages without one.
Once the code has executed, you can remove it or comment it out to avoid repeatedly setting the default image on future updates.



Charlie, thank you for a very clear tutorial on this, and especially for including an example of how to apply this to existing posts, as that’s more like what I need for my situation. I do have a particular need that I will ask about…
My wife has a number of posts that she has not chosen to put a featured image on, and she’d like to keep it that way for those. However, there is a “related posts” section at the end of her posts with thumbnails and titles. This block randomly picks posts within the category of the post they are appearing in. If one of those posts happens to be a post that didn’t have a featured image chosen, the image is dropped and the title moves into the image’s space. She’s like to have a default featured image set for those posts, but only have it show up in the “related posts” section but not show up as the featured image in the archive listings. Is this possible or is it a case of “if you want a default image, it’s going to have to show up everywhere it’s needed”? Thanks for any advice you can give if this is possible.
Hi David,
It depends on the plugin or theme your wife used to display this related post section. I can’t give you any specific advice since I don’t know which one she used. However, here is my approach to display related posts with featured images even if a post does not one (use predefined image).
Make sure to replace
'your_custom_image_url'with the URL of your custom image. You can add this code to your theme’sfunctions.php