I basically want to use WP_Query to select 3 latest posts which have featured images and this is the only way I can think of.




$query = new WP_Query( array( "post_type" => 'case-study', 'meta_key' => '_thumbnail_id', "posts_per_page" => '10', 'orderby' => 'menu_order' ) );

which doesn't work

I'm trying to add a custom field to add a hidden custom field to a post when there is a featured image attached (either true or false).

I've tried hooking into updated_post_meta, update_post_meta, wp_insert_post but I cant seem to match the $meta_key to "_thumbnail_id".


function get_latest_with_featured_image( $args = array() ) {
    $args = array_merge( array( "post_type" => 'post', "meta_key" => "_thumbnail_id", "posts_per_page" => '10', 'orderby' => 'menu_order' ), $args );
    $query = new WP_Query( $args );
    return $query->posts;

$posts = get_latest_with_featured_image( array(  "post_type" => "case-studies",  "posts_per_page" => 10 ) );
  • Would you mind editing your question to indicate what you're actually trying to accomplish? If your ultimate objective is to retrieve the three latest posts with featured iamges, please write your question accordingly. Then, you can add the other details as one implementation method that you tried unsuccessfully. – Chip Bennett Jan 8 '13 at 23:48
  • I have asked this before which people have indicated cant be done without extending the core which I dont want to do, therefor have asked a new question which will help accomplish (hopefully) a previous one – Luke Snowden Jan 8 '13 at 23:51
  • 1
    I would recommend not using the generic variable name $query for your custom query. I would name it something descriptive. But more importantly: did you add post-thumbnail support to your case-study post-type? Do you have case-study posts that have featured images attached? – Chip Bennett Jan 8 '13 at 23:52

You've provided the answer to your own question, query for meta key _thumbnail_id to get posts with a featured image:

$args = array(
    'meta_key' => '_thumbnail_id',
    'posts_per_page' => 3
$latest_with_thumbnails = new WP_Query( $args );
  • tried it, don't work... – Luke Snowden Jan 8 '13 at 23:48
  • What exactly did you try? What happened unexpectedly, or didn't happened that you expected to happen? – Chip Bennett Jan 8 '13 at 23:48
  • 1
    nothing was returned – Luke Snowden Jan 8 '13 at 23:51
  • Right my fault, custom post type was incorrect. Well, that was simple, thank you! – Luke Snowden Jan 8 '13 at 23:55

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.