Sort by posts that have a featured image?

I’d like to sort posts on the index page to show those that have a featured image first.

I’ve done this in functions.php:

Read More
   add_filter( 'pre_get_posts', 'my_get_posts' );

   function my_get_posts( $query ) {
     if ( is_home() || is_archive() ) {
       $query->set('orderby', 'meta_value');
       $query->set('meta_key', '_wp_attached_file');
     }
   }

The causes no posts to be shown on the index page.

What should I be doing here?

Related posts

Leave a Reply

1 comment