the_post_thumbnail fallback using hooks

you can use the post_thumbnail_html filter hook which passes 5 variables to your hooked function :

  • $html – the Output html of the post thumbnail
  • $post_id – the post ID.
  • $post_thumbnail_id – the attachment id of the image
  • $size – the size requested or default
  • $attr – Query string or array of attributes.

So something like:

add_filter( 'post_thumbnail_html', 'my_post_thumbnail_fallback', 20, 5 );
function my_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        // return you fallback image either from post of default as html img tag.
    }
    return $html;
}