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.

<?php
/**
* Set Featured Image Fallback
* @author Bill Erickson
* @see 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 );
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

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

Let's Talk