skip to Main Content

Fallback Post Thumbnail (Featured Image)

This snippet will allow you to set a fallback featured image anytime a featured image (post thumbnail) is requested on your WordPress site. The first function will add the post_thumbnail_id filter which doesn't exist by default in WP core and the second function adds the fallback.

/**
 * Add fallback for post thumbnail.
 *
 * @link https://total.wpexplorer.com/docs/snippets/fallback-post-thumbnail-featured-image/
 */
add_filter( 'post_thumbnail_id', function( $id ) {
    if ( is_admin() ) {
        return $id;
    }
    return $id ?: 5; // Set fallback to attachment with ID of 5
} );
All PHP snippets should be added via child theme's functions.php file or via a plugin (we recommend Code Snippets)
Back To Top