17

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

5
  • 2
    The code snippet worked for me as expected. Maybe the error is in another part of the code.
    – Ralf912
    Commented Mar 3, 2013 at 19:40
  • What do you get from var_dump( $query )? Commented Mar 3, 2013 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
    Commented Mar 3, 2013 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
    Commented Mar 4, 2013 at 0:27
  • Yes, I checked the box when importing.
    – harryg
    Commented Mar 4, 2013 at 9:16

2 Answers 2

3

When running the importer, you must check the checkbox to download and import all media/attachments, and the original server must be reachable from the server conducting the import.

If you fail to do this, the posts will have no valid featured image, and fail to show in your loop, and any attempt to call the_post_thumbnail will fail giving the impression they have no featured image.

To fix this, delete your posts, and re-import using the correct settings, making sure the old server is still accessible at its original URL.

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
        // individual statement should be on individual line
        $query->the_post();

        // you only need open/close tags here, not every line, save yourself some time typing
        ?><h2><?php the_title(); ?></h2><?php 

        // only print out the thumbnail if it actually has one
        if ( has_post_thumbnail() ) {
            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

8
  • 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. Commented Mar 4, 2013 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
    Commented Mar 4, 2013 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
    Commented Mar 4, 2013 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
    Commented Mar 4, 2013 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
    Commented Mar 4, 2013 at 10:00
31

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'
        ),
    )
);
5
  • Thanks for the suggestion. I now get the 6 posts but still get posts without thumbnails.
    – harryg
    Commented Mar 3, 2013 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. Commented Mar 4, 2013 at 0:01
  • harryg, can you confirm you're using v3.5 or later?
    – Tom J Nowell
    Commented Mar 4, 2013 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
    Commented Mar 4, 2013 at 9:15
  • 2
    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. Commented Feb 28, 2014 at 22:28

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.