Taggarkiv: default thumbnail

Add a default fallback for get_post_thumbnail / featured image

Since WordPress doesn’t have any support for a ”default thumbnail” and there isn’t any filters that works directly on get_post_thumbnail(),  the_post_thumbnail(), get_post_thumbnail_id(), get_the_post_thumbnail(), etc – we need to hook the filter get_post_metadata.

* Fall back to a hard coded default thumbnail if a thumbnail doesn't already exist
function custom_filter_get_post_metadata($metadata, $object_id, $meta_key, $single)

if(isset($meta_key) && '_thumbnail_id' === $meta_key)
//Temporarily remove filter to fetch the real data, since it's not sent as a parameter to this filter
remove_filter('get_post_metadata', 'custom_filter_get_post_metadata', true, 4);

// Get real thumbnail id
$existing_thumbnail = get_post_meta($object_id,'_thumbnail_id',true);

// Set it to a fallback ID if it doesn't already exist
if (empty($existing_thumbnail)) $result = 3372; << CHANGE THIS ID TO A ATTACHMENT ID!!!

// Re-enable the filter
add_filter('get_post_metadata', 'custom_filter_get_post_metadata', true, 4);

return $result;

return $metadata;
add_filter('get_post_metadata', 'custom_filter_get_post_metadata', true, 4);