I've been brought in to help out with development on an existing plugin, and have run into a strange issue. Not sure if I'm overlooking something obvious here.

The situation: I'm adding a shortcode that will basically spit out a list of posts of a particular custom type, complete with title/link/thumbnail. Standard stuff. The below code is giving me trouble.

        if( has_post_thumbnail() ) {
            $recipes .= get_the_post_thumbnail();

The $recipes variable is returned at the end of the function, to be output with the corresponding shortcode.

Edit with more info: The above code is within The Loop, with a custom query limiting retrieved posts to just those of the particular cpt.

All other post info is outputting correctly, but neither get_the_post_thumbnail() or the_post_thumbnail() return anything. There is a thumbnail attached to these posts in admin, has_post_thumbnail() returns true, and even get_post_thumbnail_id() works as expected. The thumbnail is definitely there.

I tried changing my query to get regular posts, rather than the custom post type, and thumbnails suddenly worked fine, so it has something to do with the cpt. I've tried making $post global, and even manually adding a post id as a parameter. No effect.

At the moment I'm just using wp_get_attachment_url( get_post_thumbnail_id() ) as a workaround but it's driving me crazy not knowing what's wrong.

What ridiculous mistake have I made?

Things I've tried:

  • Disabling all other plugins, changing to default theme(s)
  • Manually entering a valid cpt post ID as the parameter to get_the_post_thumbnail()
  • global $post; and using the ID from that
  • Try passing in the post ID to get_the_post_thumbnail($post_id, 'thumbnail'); and specify the image size you want. Check out these examples in the WordPress Codex. Sep 25, 2013 at 12:42
  • Thanks, but as mentioned above I've already tried adding in the post id as a parameter with no result. Just double checked it with the size specified too, no change.
    – Shellbot
    Sep 25, 2013 at 12:45
  • You'll need to post more context. Saying a function doesn't work without any context of how it's used doesn't give us much to go on. Most WordPress functions are not deterministic: calling the_post_thumbnail in different places on the page with the same inputs may yield different results, for instance. Depends on global state, etc. Sep 25, 2013 at 12:48
  • @Shellbot Double check the post in question indeed does have a featured image set. Then check the results of has_post_thumbnail($post_id); is it accurate? Sep 25, 2013 at 12:49
  • @MichaelEcklund It's actually all posts of the particular cpt type, rather than one in particular, but yes I've double checked that a thumbnail is set and has_post_thumbnail($post_id) definitely returns true. As I said, even get_post_thumbnail_id() is working so there's definitely a thumbnail there.
    – Shellbot
    Sep 25, 2013 at 12:53

5 Answers 5


If nothing works, give this a try:

$img_attribs = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' ); // returns an array
if( $img_attribs ) {
<img src="<?php echo $img_attribs[0]; ?>" width="<?php echo $img_attribs[1]; ?>" height="<?php echo $img_attribs[2]; ?>">
<?php } ?>
  • This is the only one that worked for me in more than one plugin, when using a custom post type.
    – Ciprian
    Apr 30 at 15:25

I had the same problem and I could not make get_the_post_thumbnail() work. However I found a workaround to display the thumbnail on custom type, just adding the code below.

<?php echo wp_get_attachment_image( get_post_thumbnail_id(), 'thumbnail' ); ?>

Posting in case it may be helpful.


Since you are trying to get the thumbnail within the loop like home page, category page or any other term page. you have to use - the_post_thumbnail();

If you want it in a specific page like single page, you should be using get_post_thumbnail_id($postId);

  • Just a tip, use backticks( on my keyboard, it is the ~ button) or the {} sign above the editor for code, don't use bold lettering :-) Thank you Feb 7, 2015 at 7:03
  • Another tip: you can print backticks by using Alt+96 (numbers are typed on the numeric keyboard, not the one above the letters)
    – Erenor Paz
    Mar 22, 2017 at 8:58

I have the same problem, but that solved:

$attachment_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image($attachment_id, 'thumbnail');

Called from the loop, this worked for me:

get_the_post_thumbnail( get_the_ID() );
  • get_the_ID() is redundant. It will use the current post in the loop by default. Jul 24, 2018 at 6:44
  • 1
    @JacobPeattie that's because calling get_the_post_thumbnail() without an argument returns an empty string, as the question so clearly states. Including the "redundant" argument makes it work, for me at least. This solution is one related to the question, not a generic one. Jul 25, 2018 at 13:32
  • get_the_ID() gets the id from literally the exact same place as the default argument for get_the_post_thumbnail() does: the global post variable. If one works but not the other then there’s a different more serious issue that should be fixed. Using get_the_ID() like this in that situation would be a hack, not a fix for the issue. Jul 25, 2018 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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