How To Set a Default Featured Image For All WordPress Posts In One Category

Featured images or post thumbnails are the images that are usually visible next to the post title.

It’s easy to add the featured image using the WordPress editor but sometimes you forget to add it or you want to use the same image for all posts in one category.

For example, I want to use the same image for all pages in the “Udemy Courses” category.

The second post looks broken because I forgot to add the featured image.

If you forget to add a featured image for a post, then it will appear without a thumbnail, and your layout will look broken.

I’ll show you how to solve this problem by configuring a fallback image that will be used if the featured image is not added to the post.

Instead of adding yet another plugin, we’ll use a simple code snippet that’s lightweight and completely customizable to fit your needs. In this example, I’ll add a default image only to one category.

You need:

  • Image or the id of the image we want to use as a default
  • Snippets plugin to run the code snippet

First, upload the image to the media library. If your image is already uploaded, just skip this step and open the image in the media library.

The fallback image opened in the Media Library

Now check the URL, the last part, after “item=” is the post thumbnail id.

We’ll use ‘post_thumbnail_html’ filter to add the image if the image is missing. In this function, we’ll first check if the image is already set, if not, we check if the post has the category we want to add. Add your category here, use the slug, you can find it easily when editing the category

How to find a category slug

Also, change the value of $default_thumbnail_id, and replace it with the ID you found in the first step.

...
	if(has_category('udemy-courses', $post->ID)){
			//https://yourdomain.com/wp-admin/upload.php?item=24116 <----ID
			$default_thumbnail_id = 294;
      ...

Sometimes, this hook won’t be called when there is no image attached to the post. That’s a good thing because that call would be useless and well-coded themes like GeneratePress won’t trigger the ‘post_thumbnail_html’ hook. I am using this theme because it’s super lightweight and has excellent performance.

To trick the WordPress and the theme into thinking that the post has the featured image, we’ll call the ‘has_post_thumbnail’ and always return true for our selected category.

function custom_has_post_thumbnail($has_thumbnail, $post, $thumbnail_id){
	if(has_category('udemy-courses', $post->ID)){
		return true;
	}else{
		return $has_thumbnail;
	}
}
add_filter('has_post_thumbnail', 'custom_has_post_thumbnail', 10, 3);

Putting all together in one snippet:

/**
	 * Set a default featured image if it is missing
	 * @param sting $html
	 * @param int $post_id
	 * @param int $post_thumbnail_id
	 * @param string $size
	 * @param array $attr
	 * @return string
	 */	
function show_default_featured_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
	
		// if an image is set return that image
		if ( $post_thumbnail_id) return $html;
		
  		//replace with your category
		if(has_category('udemy-courses', $post->ID)){
			//https://yourdomain.com/wp-admin/upload.php?item=24116 <----ID
			$default_thumbnail_id = 294;

			if (isset($attr['class']) ) {
				$attr['class'] .= " default-featured-img";
			} else {
				$size_class = $size;
				if ( is_array( $size_class )) {
					$size_class = 'size-' . implode( 'x', $size_class);
				}
				//attachment-$size is a default class `wp_get_attachment_image` would otherwise add. It won't add it if there are classes already there
				$attr = array ('class' => "attachment-{$size_class} default-featured-img");
			}
			$html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
		}

		return $html;
}
add_filter( 'post_thumbnail_html', 'show_default_featured_image', 20, 5 );


/**
	Tricking WP to think every post has a featured image. Then we use the function above to insert a default image if the featured image doesn't exist
*/
function custom_has_post_thumbnail($has_thumbnail, $post, $thumbnail_id){
	
	if(has_category('udemy-courses', $post->ID)){
		return true;
	}else{
		return $has_thumbnail;
	}
	
}
add_filter('has_post_thumbnail', 'custom_has_post_thumbnail', 10, 3);

And the final result:

Both posts now have a featured image

The first post in the list has the featured image configured but the second one doesn’t. The snippet is adding that image on the fly. Now I can add hundreds of Udemy-related posts with the same featured image without the need to upload the same image every time.

Still, if I want to add a different image to one specific post, I can do it simply by configuring the featured image on that specific post in the WordPress editor.

Leave a Comment