Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to query 6 posts, but only those which have a featured image attached. I am using meta_key method for this with WP_Query as such:

$args = array(
    'post_type'  => 'post',
    'meta_key' => '_thumbnail_id',
    'post_count' => 6 );
$query = new WP_Query($args);

Followed by

<?php while($query->have_posts()) : $query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_post_thumbnail('thumbnail'); ?>
<?php endwhile; ?>

This doesn't seem to work. The query is returning more than 6 posts and also those which don't have a featured image. Any ideas what I've got wrong here?

@chip: var_dump($query); gives a large result so I have posted it in a pastebin

share|improve this question
2  
The code snippet worked for me as expected. Maybe the error is in another part of the code. –  Ralf912 Mar 3 '13 at 19:40
    
What do you get from var_dump( $query )? –  Chip Bennett Mar 3 '13 at 20:07
    
As a extra thing I thought of - the posts were imported to my local server from a live site using wordpress exporter/importer. The original posts all had featured images which were obviously lost in the import process. Could that mean that although the featured images were removed the meta keys still remained causing the query to mess up? –  harryg Mar 3 '13 at 23:39
    
When you ran the import, did you check the box to download and attach all attachments/media? If you did not then your featured images will not work. The posts will have a featured image, but it will be referencing an attachment that does not exist, and so will never be displayed –  Tom J Nowell Mar 4 '13 at 0:27
    
Yes, I checked the box when importing. –  harryg Mar 4 '13 at 9:16
add comment

2 Answers 2

up vote 2 down vote accepted

When running the importer, you must check the checkbox to download and import all media/attachments.

If you do not, the posts will still have a featured image set, but the attachment they refer to will be invalid, and any attempt to call the_post_thumbnail will fail. Using your code you would get just the post title, and no thumbnail. This would give the impression that posts that have no featured image are being pulled in. This is not the case, they have a featured image, it's just not available because of the mistake you made when importing.

Delete your posts, and re-import using the correct settings.

Also to demonstrate the point, you made no attempt to actually check the posts, always check e.g.:

if ( $query->have_posts() ) { // you never checked to see if no posts were found
    while($query->have_posts()) { // alt style syntax doesn't work with most IDEs
        $query->the_post(); // individual statement should be on individual line
        ?><h2><?php the_title(); ?></h2><?php // you only need open/close tags here, not every line, save yourself some time typing
        if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
            echo '<p>post says it has a featured image</p>'; // double checking
            the_post_thumbnail('thumbnail');
        } else {
            echo '<p>this post does not have a featured image</p>';
        }
    }
} else {
    echo '<p>no posts found</p>';
}

If you have not checked the checkbox, you will get images saying they have a featured image, but no image is shown

share|improve this answer
1  
I suspect this is the reason the answer I provided isn't working as expected. the WordPress Suicide plugin is pretty helpful for deleting all content. –  helgatheviking Mar 4 '13 at 4:48
    
I definitely did check that box. However in my experience it has never worked for me. All the images were linked back to the external site. I used the Import External Images(wordpress.org/extend/plugins/import-external-images) plugin to store the images locally and attach. –  harryg Mar 4 '13 at 9:14
    
I've had the same thought, I definitely checked the box, why does it not work still? I've had it from clients, and it's so easy to get wrong, so delete your posts, re-import and make sure it's checked, and if it still doesn't work then you can rule it out. Otherwise there's a Neil DeGrasse Tyson talk about how pathetically weak human memory is. Right now I could ask you to uncheck the second tickbox and you'd probably remember seeing that too ( but there isn't one ) –  Tom J Nowell Mar 4 '13 at 9:52
    
and in the meantime, wrap the_post_thumbnail with a has_thumbnail call and add an else case so that you definitely unambiguously know that the post doesn't have a featured image –  Tom J Nowell Mar 4 '13 at 9:53
    
Also make sure your server is able to reach the remote server. If your server is unable to grab remote requests, it will be unable to import. You may well need to delete the media attachments already grabbed –  Tom J Nowell Mar 4 '13 at 10:00
show 3 more comments

As of WordPress 3.5, the Meta Query Parameters support EXISTS and NOT EXISTS as compare operators. In the case where you are using these, you can omit the value part of the meta query. Try the following arguments for your query:

$args = array(
   'post_type'  => 'post',
   'posts_per_page' => 6,
    'meta_query' => array(
        array(
         'key' => '_thumbnail_id',
         'compare' => 'EXISTS'
        ),
    )
);
share|improve this answer
    
Thanks for the suggestion. I now get the 6 posts but still get posts without thumbnails. –  harryg Mar 3 '13 at 23:36
    
I tested this and it worked for me. But I did notice that it will return entities that have the meta set, but for some reason that attachment ID isn't correct. So double check that your posts have the featured images set. Otherwise, I don't know what else the problem could be. –  helgatheviking Mar 4 '13 at 0:01
    
harryg, can you confirm you're using v3.5 or later? –  Tom J Nowell Mar 4 '13 at 0:25
    
Yes I am running the latest version of WordPress - freshly installed on my local dev server as a network (multisite) setup. –  harryg Mar 4 '13 at 9:15
    
Just some additional information: There is a unresolved bug, see trac ticket 23268, which makes it necessary to use a value if NOT EXISTS is used. –  ialocin Feb 28 at 22:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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