Well it turns out that the reason has_post_thumbnail()
was failing was because get_post_meta()
was returning empty for scheduled posts. I’m still not sure why, but in case someone else has the issue, my workaround was to create a new function to fetch the featured image ID without relying on get_post_meta()
:
function get_featured_image_id($postID) {
global $wpdb;
$data = $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM wp_postmeta WHERE post_id = %d AND meta_key = '_thumbnail_id'", $postID));
if (!empty($data[0]->meta_value)) {
return $data[0]->meta_value;
}
}
Then you can get a featured image URL in your template with the following line:
wp_get_attachment_url(get_featured_image_id($post->ID))