wp query – Sort by presence of thumbnail

Question

Looking to ORDER BY the result of WP_Query based on presence of post thumbnail.

  $query_args = [
    'post_type'     => 'custom_project',
    'post_status'   => 'publish',
    'orderby'   => 'meta_value_num',
    'meta_key'  => '_thumbnail_id'
  ];


  $query_args['tax_query'] = array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'set',
        'field'    => 'slug',
        'terms'    => 'some_set'
    )
  );

Producing:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  \
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) \
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (322)
) AND ( 
  wp_postmeta.meta_key = '_thumbnail_id'
) AND wp_posts.post_type="custom_project" AND ((wp_posts.post_status="publish"))  \
GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 10

This only returns posts that contain a featured image. I have unsuccessfully tried variations with with compare values and including '_thumbnail_id' in a second tax query array and am considering just using array_sort after the fact, but hoping for a more elegant solution.

0
MikeiLL 1 month 2022-07-17T11:39:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse