WP_Query has_post_thumbnail() returning false for all posts in custom loop, even when Featured Image is set.
Hi everyone, I’m running into a baffling issue while building a custom post ticker shortcode.
My WP_Query successfully fetches the latest posts and grabs their titles and permalinks perfectly. However, has_post_thumbnail() is returning false for every single post in the loop, forcing my code to output my text fallback instead of the images.
I know for a fact that all the post have feature images
Here is my code (running via Code Snippets plugin):
add_action( 'after_setup_theme', function() {
add_theme_support( 'post-thumbnails' );
add_image_size( 'blog-card', 600, 400, true );
} );
add_shortcode('custom_post_ticker', 'build_smooth_ticker'); 1
function build_smooth_ticker() {
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish'
));
if (!$query->have_posts()) return '';
$items = '';
while ($query->have_posts()) {
$query->the_post();
$current_id = get_the_ID();
// TEMPORARY DEBUG LINE
$items .= '<!-- ID: ' . $current_id . ' | Has thumb: ' . (has_post_thumbnail($current_id) ? 'YES' : 'NO') . ' -->';
if (has_post_thumbnail($current_id)) { $content = get_the_post_thumbnail($current_id, 'blog-card'); } else {
// This fallback always triggers
$content = '<div style="color: white; font-weight: bold; padding: 0 20px; line-height: 180px;">' . get_the_title() . '</div>'; }
$items .= '<div class="ticker-item"><a href="' . get\_permalink() . '">' . $content . '</a></div>';
}
wp_reset_postdata();
return '<div class="ticker-wrap"><div class="ticker-track">' . $items . $items . '</div></div>'; }
[link] [comments]