# Default Featured Image

> Uses an image of your choice as the featured image for posts that do not have one, so archive grids, related posts and social shares never show a gap.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: featured-image, fallback, images
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/default-featured-image/

## Code

```php
// Attachment ID of the fallback image.
$wpide_default_thumbnail_id = 0;
// Post types that use the fallback.
$wpide_default_thumbnail_types = array( 'post' );

add_filter( 'post_thumbnail_id', function ( $thumbnail_id, $post ) use ( $wpide_default_thumbnail_id, $wpide_default_thumbnail_types ) {
    if ( $thumbnail_id || ! $wpide_default_thumbnail_id || is_admin() || ! $post ) {
        return $thumbnail_id;
    }

    return in_array( get_post_type( $post ), $wpide_default_thumbnail_types, true ) ? (int) $wpide_default_thumbnail_id : $thumbnail_id;
}, 10, 2 );
```

## Notes

Upload the fallback image, copy its attachment ID from the Media Library and put it in $wpide_default_thumbnail_id.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Default Featured Image" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
