Featured Image Fallback

If you have another meta field that is storing an image (like a Header Image upload field), it might be a good idea to set the featured image fallback to that. This is especially useful when using Yoast SEO for Facebook Open Graph tags, as it uses the featured image for the og:image property.

This code uses the meta field ea_page_header as the featured image if no featured image has been set.

/**
 * Set Featured Image Fallback
 * @author Bill Erickson
 * @link http://www.billerickson.net/code/featured-image-fallback/ 
 */
function ea_featured_image_fallback( $value, $post_id, $meta_key, $single ) {
	if( $single && '_thumbnail_id' == $meta_key && empty( $value ) )
		$value = get_post_meta( $post_id, 'ea_page_header', true );
	return $value;
}
add_filter( 'get_post_metadata', 'ea_featured_image_fallback', 10, 4 );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk