Skip to content

Instantly share code, notes, and snippets.

@contemplate
Created December 15, 2019 23:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save contemplate/5b48c00bdc659f9a24e3632f823ee6d5 to your computer and use it in GitHub Desktop.
Save contemplate/5b48c00bdc659f9a24e3632f823ee6d5 to your computer and use it in GitHub Desktop.
MyBookTable: use book image as post thumbnail
/**
* MyBookTable: use book image as post thumbnail
*/
//Find Book Image ID
add_filter( 'get_post_metadata', 'mbt_use_book_image_as_thumbnail', 10, 4 );
function mbt_use_book_image_as_thumbnail( $null, $object_id, $meta_key, $single ) {
if ( '_thumbnail_id' !== $meta_key ) {
return $null;
}
if ( is_admin() ) {
return $null;
}
if ( get_post_type( $object_id ) == 'mbt_book' ) {
remove_filter( 'get_post_metadata', __FUNCTION__, 10, 4 );
$featured_image_id = get_post_meta( $object_id, 'mbt_book_image_id', true );
add_filter( 'get_post_metadata', __FUNCTION__, 10, 4 );
// The post has a featured image.
if ( $featured_image_id ) {
return $featured_image_id;
}
}
return $null;
}
// Set MBT Thumbnail size to Large
add_filter( 'post_thumbnail_size', 'mbt_post_thumbnail_size', 10, 2);
function mbt_post_thumbnail_size( $size, $post_id ) {
if ( get_post_type( $post_id ) == 'mbt_book' ) {
$size = 'large'; // Change to your desired image size.
}
return $size;
}
@contemplate
Copy link
Author

This code returns the MyBookTable Book Image ID when theme & plugins are calling the has_post_thumbnail or get_the_post_thumbnail functions for the mbt_book custom post type.

@BerkleyR
Copy link

This is brilliant :) just used it and it worked like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment