How to filter wp_get_recent_posts() to only posts that have thumbnails?

by JPB   Last Updated July 20, 2017 06:08 AM

I made a script that shows recent posts. But here's the problem, I want to show only posts that have thumbnails, and I want to show at least 10 posts if there are that many without thumbnails. So the filtering needs to happen before fetching 10 recent posts, because otherwise there's a possibility that none of them could have thumbnails and nothing will show.

Here's my code:

function add_before_my_siderbar() {
  // get recent posts
  $recent_posts = wp_get_recent_posts();
  foreach( $recent_posts as $recent ) {

       // Print recent posts

  }
}

add_action( 'get_my_sb_widget', 'add_before_my_siderbar' );


Related Questions