The has_post_thumbnail WordPress PHP filter checks if a post has a post thumbnail.
Usage
add_filter('has_post_thumbnail', 'your_function_name', 10, 3);
function your_function_name($has_thumbnail, $post, $thumbnail_id) {
// your custom code here
return $has_thumbnail;
}
Parameters
$has_thumbnail(bool): true if the post has a post thumbnail, otherwise false.$post(int|WP_Post|null): Post ID or WP_Post object. Default is global$post.$thumbnail_id(int|false): Post thumbnail ID or false if the post does not exist.
More information
See WordPress Developer Resources: has_post_thumbnail
Examples
Hide thumbnails for specific categories
This example hides post thumbnails for posts in the “No Thumbnail” category.
add_filter('has_post_thumbnail', 'hide_thumbnail_for_category', 10, 3);
function hide_thumbnail_for_category($has_thumbnail, $post, $thumbnail_id) {
if (has_category('No Thumbnail', $post)) {
return false;
}
return $has_thumbnail;
}
Force thumbnail for specific post types
This example forces a thumbnail for all posts of the custom post type “events”.
add_filter('has_post_thumbnail', 'force_thumbnail_for_events', 10, 3);
function force_thumbnail_for_events($has_thumbnail, $post, $thumbnail_id) {
if (get_post_type($post) == 'events') {
return true;
}
return $has_thumbnail;
}
Hide thumbnails for posts by specific authors
This example hides post thumbnails for posts written by the author with ID 5.
add_filter('has_post_thumbnail', 'hide_thumbnail_for_author', 10, 3);
function hide_thumbnail_for_author($has_thumbnail, $post, $thumbnail_id) {
if (get_post_field('post_author', $post) == 5) {
return false;
}
return $has_thumbnail;
}
Show thumbnails for posts with a specific custom field
This example shows post thumbnails for posts that have a custom field “show_thumbnail” set to “yes”.
add_filter('has_post_thumbnail', 'show_thumbnail_for_custom_field', 10, 3);
function show_thumbnail_for_custom_field($has_thumbnail, $post, $thumbnail_id) {
if (get_post_meta($post->ID, 'show_thumbnail', true) == 'yes') {
return true;
}
return $has_thumbnail;
}
Hide thumbnails for password-protected posts
This example hides post thumbnails for password-protected posts.
add_filter('has_post_thumbnail', 'hide_thumbnail_for_protected_posts', 10, 3);
function hide_thumbnail_for_protected_posts($has_thumbnail, $post, $thumbnail_id) {
if (post_password_required($post)) {
return false;
}
return $has_thumbnail;
}