WordPress.org

Make WordPress Core

Opened 13 months ago

Last modified 13 months ago

#23983 new enhancement

Add filter to get_post_thumbnail_id to override default thumbnail use

Reported by: Jesper800 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: 2nd-opinion
Focuses: Cc:

Description

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.

Change History (1)

comment:1 alexvorn213 months 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.

Last edited 13 months ago by alexvorn2 (previous) (diff)
Note: See TracTickets for help on using tickets.