Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

   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?

share|improve this question
add comment

1 Answer 1

Change _wp_attached_file to _thumbnail_id

share|improve this answer
    
Close! With this, only posts that have a thumbnail show up. I'd like all posts to be listed, but those with a thumbnail to be listed first. –  Laizer Jul 22 '12 at 16:56
    
Unfortunately you can't query posts that both do & don't have a meta key - it's the nature of the join coupled with the schema. You'll have to run two queries, one for all with, the other without. –  TheDeadMedic Jul 22 '12 at 17:35
    
no outer joins? –  Laizer Jul 22 '12 at 19:22
    
You'll have to ask an SQL guru, but I'm pretty sure it can't be done (at least efficiently) with the design of metadata tables. –  TheDeadMedic Jul 22 '12 at 19:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.