I need to display max 5 latest sticky posts that have a thumbnail. And I need to have accurate counting of the displayed posts. I’ve tried to exclude sticky posts without a thumbnail with meta_query, but with no luck.
$sticky = get_option('sticky_posts');
if (empty($sticky)) {
return;
}
$counter = 1;
$r = new WP_Query(array(
'posts_per_page' => 5,
'post__in' => $sticky,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS',
),
),
'post_status' => 'publish',
'orderby' => 'post__in',
'post_type' => array( 'post' ),
));
if ($r->have_posts()) :
echo '<section class="header-sticky-posts '.$post_count.'">';
while ( $r->have_posts() ) : $r->the_post();
echo '<div class="header-sticky-post">';
// Post content
echo '</div>';
$counter++;
endwhile;
echo '</section>';
endif;
wp_reset_postdata();