2

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

You need to set ignore_sticky_posts to true in your query arguments. This way you exclude sticky posts and only focus on the post ID's array being passed to post_in

'ignore_sticky_posts' => true,

EDIT

If this does not make much sense, please see my answer here to similar question where I have explained it a bit better. Be sure to check it out

THE CODE

$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' ),
    'ignore_sticky_posts' => true,
));
| improve this answer | |
  • See my updated answer – Pieter Goosen Jul 16 '15 at 15:51
  • My pleasure, glad it worked. Enjoy :-) – Pieter Goosen Jul 16 '15 at 15:56
  • Do you know if there is any difference if I use 'meta_key' => '_thumbnail_id' instead of meta_query? Both seem to work. – Nikita Jul 16 '15 at 16:05
  • 1
    There is no difference. Both syntaxes are correct and will work. The only real difference is in that with a meta_query you can build nested and more complex queries which you can't do with the meta_* syntax :-) – Pieter Goosen Jul 16 '15 at 16:09
0

What you are doing is right except that you are careless or you just copied code from somewhere else and pasted in without modifying it.

<?php
$sticky = get_option('sticky_posts');

if (empty($sticky)) {
    return;
}

$counter = 1;

$posts = 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 ($posts->have_posts()) :

        while ( $posts->have_posts() ) : $posts->the_post();

                if ( has_post_thumbnail() ) :
                    echo '<section class="header-sticky-posts'.$counter.'">';
                    echo '<div class="header-sticky-post">';
                            the_post_thumbnail();
                    echo '</div>';
                    echo '</section>';
                    $counter++; 
                endif;

        endwhile;

endif;
wp_reset_postdata();
| improve this answer | |
  • This is my code. You could be more specific with your answer. I see that you propose to check if there is a thumbnail in the loop. But it's not what I was looking for. Yes, sticky posts without thumb will not be displayed. But they will still be counted in the query. – Nikita Jul 16 '15 at 14:17
  • you use $r->have_post() that is wrong. and also you put a slash before WP_Query which should not be there. You want to display five sticky post, and the code perfectly displays the 5 sticky posts. So what is it you want ? – Puni Jul 16 '15 at 14:23
  • Why is $r wrong? :) There is a slash before WP_Query because I use PHP namespaces in my project, and I just forgot to delete it when posted here. It has nothing to do with the question. Firstly, I want to display 5 sticky posts only with thumbnails. So if we have 5 sticky posts in total, and 4 of them are with thumbnails, this code must output this 4 posts only. Your solution works here. Secondly, I need to keep post counting accurate. So if the post is not displayed, it must not be counted. Your solution fails here. Your remarks are not related to the question, sorry. – Nikita Jul 16 '15 at 14:37
  • You set $posts as query variable and you are using $r for execution it does not make sense to me. To your second question put the counter variable inside the if condition if ( has_post_thumbnail() ) : echo '<div class="header-sticky-post">'; the_post_thumbnail(); echo '</div>'; $counter++; endif; – Puni Jul 16 '15 at 14:47
  • It was just a typo, sorry. Your suggestion to put $counter++ inside "if statement" doesn't seem to do the job. – Nikita Jul 16 '15 at 15:39

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.