Option 1: force featured image on all posts
Use the save_post
hook, which runs twice per “save” action if you have revisions enabled. You can verify it’s the final run by checking that there is $_POST
data (it’s empty the first time around).
add_action('save_post', 'wpse_force_featured_image', 20, 2);
function wpse_force_featured_image($post_id, $post) {
// If this is a "Post" and $_POST data is present
if(count($_POST) > 0 && $post->post_type == 'post') {
// Check if featured image is missing
if(!isset($_POST['_thumbnail_id'])) {
// Add your desired fallback featured image
add_post_meta($post_id, '_thumbnail_id', 'my_fake_id');
}
}
}
You’ll have to make sure that the ID is a real image in the Media Library. Also, this will only work for new Posts, and Posts you update after the code is added. Any old Posts will have to be updated one by one.
Option 2: update theme to include fallback
Wherever you’re using the featured image in the theme, you could just add a fallback image. So for example, if in single-post.php
you have something like
if(has_post_thumbnail()) {
the_post_thumbnail();
}
you can change this to
if(has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img src="https://wordpress.stackexchange.com/questions/356627/image_url.jpg" alt="alt text" />';
}
The benefit is, this will add a fallback immediately for all Posts – you won’t have to go back and update them or add new ones to see this in action. (If you don’t already have a child theme or a custom theme, you’ll want to make one so you’re not editing parent theme files.)