0

I am making some changes to a custom WP template and have hit an odd issue. Published posts work absolutely fine, but when you view a scheduled post the Featured Image disappears.

It's as if there was no featured image at all and has_post_thumbnail() returns false. Here's the relevant piece of code I'm working with, relatively simple stuff really:

while (have_posts()) : the_post();
  if (has_post_thumbnail($post->ID)) {
    $featuredImage = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
  }
}

And again, this works fine on published posts but as soon as I set the date to the future the image stops coming through. It seems strange that images would be treated differently based on the publish date, is there anything which WP changes on scheduled posts vs live posts which could be causing this?

  • I can't reproduce this issue, your code works for me with a scheduled post. verify it's not a theme or plugin related issue by disabling plugins and using a core theme. – Milo Jan 9 '15 at 3:28
  • 1
    Although it's working for me but why are you supplying post ID if you are using function inside the loop. You can simply use this if ( has_post_thumbnail() ) { the_post_thumbnail(); } – Robert hue Jan 9 '15 at 6:01
  • @Roberthue It hink you should post your comment as answer with a littel expanded explanation. – cybmeta Jan 9 '15 at 7:33
  • @Roberthue I have tried with and without, doesn't seem to make a difference, there must be something strange in the functions.php or a plugin perhaps, I'll keep digging. – Andrew Jan 9 '15 at 8:19
  • It seems that the reason for has_post_thumbnail() failing is because get_post_meta() returns empty, still looking in to why that is! – Andrew Jan 15 '15 at 4:01
0

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))
-2

Its too much simple

while (have_posts()) : the_post();
    if (has_post_thumbnail($post->ID)) {
        $featuredImage = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID,'full'));
        echo $featuredImage;
    }
}
  • Please add a proper explanation to how your code work and how it should be used. Its too much simple is not a valid explanation. Please file an edit – Pieter Goosen Jan 9 '15 at 8:57
  • You also have syntax errors in your code, you cannot use : and curlies together, you either use {} or : and endXXX – Pieter Goosen Jan 9 '15 at 9:14
  • ok @PieterGoosen – Irfan Jan 9 '15 at 9:18
  • The issue is that has_post_thumbnail() returns false for scheduled posts. Unfortunately this solution doesn't address that. – Andrew Jan 12 '15 at 2:52
  • @AndrewSpear There minnar problem use " ;(sc) " and not start Brakets ok please open your eyes – Irfan Jan 12 '15 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.