Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active January 5, 2023 06:42
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save westonruter/5808015 to your computer and use it in GitHub Desktop.
Save westonruter/5808015 to your computer and use it in GitHub Desktop.
How to filter the response for get_post_thumbnail_id()
<?php
/**
* How to filter the value that would be returned by get_post_thumbnail_id()
*/
add_filter( 'get_post_metadata', function ( $value, $post_id, $meta_key, $single ) {
// We want to pass the actual _thumbnail_id into the filter, so requires recursion
static $is_recursing = false;
// Only filter if we're not recursing and if it is a post thumbnail ID
if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
$is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
$value = get_post_thumbnail_id( $post_id );
$is_recursing = false;
$value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
if ( ! $single ) {
$value = array( $value );
}
}
return $value;
}, 10, 4);
<?php
// Copied from core for reference
/**
* Retrieve metadata for the specified object.
*
* @since 2.9.0
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
* the specified object.
* @param bool $single Optional, default is false. If true, return only the first value of the
* specified meta_key. This parameter has no effect if meta_key is not specified.
* @return string|array Single metadata value, or array of values
*/
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
if ( !$meta_type )
return false;
if ( !$object_id = absint($object_id) )
return false;
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
if ( null !== $check ) {
if ( $single && is_array( $check ) )
return $check[0];
else
return $check;
}
$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
if ( !$meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[$object_id];
}
if ( !$meta_key )
return $meta_cache;
if ( isset($meta_cache[$meta_key]) ) {
if ( $single )
return maybe_unserialize( $meta_cache[$meta_key][0] );
else
return array_map('maybe_unserialize', $meta_cache[$meta_key]);
}
if ($single)
return '';
else
return array();
}
<?php
// Copied from core for reference
/**
* Retrieve Post Thumbnail ID.
*
* @since 2.9.0
*
* @param int $post_id Optional. Post ID.
* @return int
*/
function get_post_thumbnail_id( $post_id = null ) {
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
return get_post_meta( $post_id, '_thumbnail_id', true );
}
<?php
// Copied from core for reference
/**
* Retrieve post meta field for a post.
*
* @since 1.5.0
* @uses $wpdb
* @link http://codex.wordpress.org/Function_Reference/get_post_meta
*
* @param int $post_id Post ID.
* @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
* @param bool $single Whether to return a single value.
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single
* is true.
*/
function get_post_meta($post_id, $key = '', $single = false) {
return get_metadata('post', $post_id, $key, $single);
}
@adamromanowski
Copy link

Really cool. Seemed really basic to filter thumbnail ID function until I saw it's connected to the function that gets all post meta. Luckily I stumbled upon your solution. Works like a charm. Many thanks sir! 🍻

@wpexplorer
Copy link

Just a heads up that the post_thumbnail_id filter is now available in core since WP 5.9 - yay!

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