Make WordPress Core

Opened 5 years ago

Last modified 6 weeks ago

#23983 reviewing enhancement

Add filter to get_post_thumbnail_id to override default thumbnail use

Reported by: Jesper800 Owned by: SergeyBiryukov
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: Media Keywords: 2nd-opinion needs-testing has-patch
Focuses: Cc:


The current function for getting the post thumbnail ID, used in among others get_the_post_thumbnail, is as follows:

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 );

In my opinion, this needs a filter, so the user can override this by the attachment of his choosing, such as an Advanced Custom Fields image attached to the post.

Something like:

function get_post_thumbnail_id( $post_id = null ) {
	$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
	return apply_filters( 'post_thumbnail_id', get_post_meta( $post_id, '_thumbnail_id', true ), $post_id );

I know you can hook into the get_{$meta_type}_metadata filter, but getting the post thumbnail ID should still be possible by using get_post_meta, the other thumbnail should just be used for displaying.

Attachments (2)

23983.patch (548 bytes) - added by gilbitron 3 years ago.
23983.2.patch (814 bytes) - added by sebastian.pisula 14 months ago.

Download all attachments as: .zip

Change History (12)

#1 @alexvorn2
5 years ago

this needs a filter, so the user can override this by the attachment of his choosing, such as an Advanced Custom Fields image attached to the post

This can confuse people. Someone then will don't get why the image, he or she is using as a featured image - will get replaced by another one.

Version 1, edited 5 years ago by alexvorn2 (previous) (next) (diff)

#2 @engelen
4 years ago

  • Keywords has-patch added

(formerly Jesper800)

Why would this confuse people? Hooking directly into get_post_meta would confuse people, but allowing a function return value to be filterable shouldn't. We're requesting the featured image for a post, not a raw database value. Allowing get_the_title to be filterable doesn't confuse people either, does it?

In my opinion this should be filterable, as it provides flexibility in a currently quite rigid thumbnail system. For example, one might want to use the thumbnail of a parent post if the post itself doesn't have one. Adding a filter adds more flexibility, there are more than enough realistic use cases, and it doesn't add any confusion.

In dire need of a second opinion :-).

#3 @wonderboymusic
3 years ago

  • Keywords needs-patch added; has-patch removed

3 years ago

#4 @gilbitron
3 years ago

  • Keywords needs-testing added; needs-patch removed

#5 @sebastian.pisula
14 months ago

Second example: Via filter we can set the default thumbnail if post not set featured image

#6 @sebastian.pisula
14 months ago

  • Keywords has-patch added

#7 @SergeyBiryukov
13 months ago

Related: #40547 (suggests a filter for get_the_post_thumbnail_url()).

#8 @sebastian.pisula
13 months ago

I haven't control to overwrite meta value via get_post_metadata filter. Why? Because I can't check that post has _thumbnail_id.

In my filter I can check:

if (empty ($ post_thumbnail_id)) {
$post_thumbnail_id = 5;

return $post_thumbnail_id;

Additional solution is compatibility with has_post_thumbnail function :-)

#9 @leogermani
8 months ago

+1 for this

hooking in get_%type%_metadata is messy, because you can not easily check wether the post has a post thumbnail or not inside your filter, you would end up in an endless loop if you call has_post_thumbnail()

In that case you have to do something like inside your callback:

$wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $object_id");

not really beautiful.

#10 @SergeyBiryukov
6 weeks ago

  • Milestone changed from Awaiting Review to 5.0
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing
Note: See TracTickets for help on using tickets.