• I’m using get_posts with the following args:

    $args=array('numberposts'=>2,'post_type'=>'post','orderby'=>'rand');

    I’d like to only get posts that have a thumbnail. Is there a way to pass has_post_thumbnail as an argument? I know it’s possible to use get_option within the array and I’ve tried adding this:

    'include'=>get_option('has_post_thumbnail')

    which does not cause an error—but doesn’t work either. Anyone know how to pull this off?

Viewing 5 replies - 1 through 5 (of 5 total)
  • as discussed here: http://wordpress.org/support/topic/display-the-post-only-if-thumbnail-is-set-up?replies=17

    add this to your args array:
    'meta_key' => '_thumbnail_id'

    Thread Starter ryanve

    (@ryanve)

    Brilliant—thank you!

    It works, except now I can’t seem to get it to show more than 3 posts.
    If I set ‘numberposts’=>1 it shows 1. If I set ‘numberposts’=>2, it shows 2. If I set ‘numberposts’=>3 or higher it shows 3. Also they’re not the more recent three, which (I think) they should be by default.

    $args = array(
    'numberposts'=>5,            // should show 5 but only shows 3
    'post_type'=>'post',         // posts only
    'meta_key'=>'_thumbnail_id', // with thumbnail
    'exclude'=>$post->ID         // exclude current post
    );

    the code works on my local wp3.1.3 install; no problem showing 5 posts.

    do all your posts, that you expect to see, have a featured image attached?

    you could try the new query parameter for custom fields:
    http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
    according to that chapter, ‘meta_key’ seems to become deprecated;

    or possibly an interference of some other code or plugins.

    Thread Starter ryanve

    (@ryanve)

    Nice one! Changing meta_key to meta_query did it. Thank you =)

    $args = array(
    'numberposts'=>5,              // show 5 posts
    'post_type'=>'post',           // posts only
    'meta_query'=>'_thumbnail_id', // with thumbnail
    'exclude'=>$post->ID           // exclude current post
    );
    Thread Starter ryanve

    (@ryanve)

    Doh! Or maybe not. The above actually doesn’t work. When I added orderby randomize posts w/o thumbnails shows up too. I tried meta_query as an array too. I’m back to this just showing 3, but I might just take out the thumbnail arg and just show the recent ones, all of which do have thumbs (added via ‘set featured image’)

    $args=array(
    	'numberposts'=>5,		//(should) show 5 posts
    	'post_type'=>'post',		//posts only
    	'meta_key'=>'_thumbnail_id', 	//with thumbnail
    	'exclude'=>$post->ID,		//exclude current post
    	'orderby'=>'rand'		//randomize
    	);
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_posts with thumbnail only’ is closed to new replies.