Có, có một cách để làm điều đó … Chúng ta hãy xem xét has_post_thumbnail
chính hàm:
function has_post_thumbnail( $post = null ) {
return (bool) get_post_thumbnail_id( $post );
}
Như bạn có thể thấy, tất cả những gì nó thực sự làm là nhận được ID hình thu nhỏ của bài đăng và kiểm tra xem nó có tồn tại hay không. Nhưng không có bộ lọc nào ở đây. Hãy đi sâu hơn:
function get_post_thumbnail_id( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
return get_post_meta( $post->ID, '_thumbnail_id', true );
}
Vẫn không có bộ lọc, nhưng có một hy vọng:
function get_post_meta( $post_id, $key = '', $single = false ) {
return get_metadata('post', $post_id, $key, $single);
}
Và cuối cùng trong get_metadata
…
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/**
* Filters whether to retrieve metadata of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 3.1.0
*
* @param null|array|string $value The value get_metadata() should return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param bool $single Whether to return only the first value of the specified $meta_key.
*/
$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;
}
...
Có vẻ như chúng ta có thể sử dụng get_post_metadata
hook để ghi đè has_post_thumbnail
kết quả. Điều duy nhất bạn phải nhớ là nó sẽ thay đổi hành vi của get_post_thumbnail_id
…
Một cái gì đó như thế này sẽ thực hiện thủ thuật:
function my_override_has_post_thumbnail( $result, $object_id, $meta_key, $single ) {
if ( '_thumbnail_id' === $meta_key ) {
// perform your checks and return some ID if thumbnail exists
}
return $result;
}
add_filter( 'get_post_metadata', 'my_override_has_post_thumbnail', 10, 4 );