Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.
Filters the post thumbnail ID.
apply_filters( 'post_thumbnail_id', int|false $thumbnail_id, int|WP_Post|null $post )
This is a filter hook that filters the post thumbnail ID. It consists of two parameters: one is the thumbnail ID or false if the post does not exist, and the second is the Post ID or WP_Post object.
It is used in get_post_thumbnail_id(), which retrieves the thumbnail ID.
This example shows how to modify the post thumbnail ID to a custom ID if certain conditions are met.
add_filter('weplugins_post_thumbnail_id', 'weplugins_custom_thumbnail_id', 10, 2);
function weplugins_custom_thumbnail_id($thumbnail_id, $post) {
if($post->post_type == 'custom_post_type') {
return 123; // Custom thumbnail ID
}
return $thumbnail_id;
}
Use this example to set a default thumbnail ID when the post does not have one.
add_filter('weplugins_post_thumbnail_id', 'weplugins_default_thumbnail_id', 10, 2);
function weplugins_default_thumbnail_id($thumbnail_id, $post) {
return $thumbnail_id ? $thumbnail_id : 456; // Default thumbnail ID
}
In this example, we adjust the thumbnail ID based on the author’s ID.
add_filter('weplugins_post_thumbnail_id', 'weplugins_author_based_thumbnail_id', 10, 2);
function weplugins_author_based_thumbnail_id($thumbnail_id, $post) {
if($post->post_author == 1) {
return 789; // Thumbnail ID for author with ID 1
}
return $thumbnail_id;
}
If you need any customization or have queries, feel free to Contact Us.
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.