I’m using get_posts with the following args:
which does not cause an error—but doesn’t work either. Anyone know how to pull this off?
add this to your args array:
'meta_key' => '_thumbnail_id'
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:
according to that chapter, ‘meta_key’ seems to become deprecated;
or possibly an interference of some other code or plugins.
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 );
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 );
- The topic ‘get_posts with thumbnail only’ is closed to new replies.