Sort by Featured Image in WordPress

Featured Images on a WordPress page or post can make it easy to use a single image to represent the content of your page. Given that they look better, why not feature them at the top of your content, while still preserving the rest of your ORDER BY? The answer is that it’s not terribly straightforward but you do have a couple of options. We can make use of the “_thumbnail_id” meta_key that is stores the attachment ID of the featured image, but as it is missing for pages and posts that don’t have a featured image they are omitted from The Loop entirely, which is not what we want.

Filters on WP_Query

The most robust way to sort your posts by featured image while not interfering with your existing ORDER BY is to plug into the filters for posts_fields_requestposts_join_request, and posts_orderby_request. The SQL that is going to be run passes through these filters in pieces, allowing us to modify it – and make sure that pages and posts without featured images are last! Since we don’t want to modify every WP_Query, presumably just some of them, we can add another argument to handle this for us – we’ll call it featured_image_sort.

class Featured_Image_Sort {
	public static function init(){
		add_filter( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
	}

	public static function pre_get_posts( $query ){
		if ( $query->query_vars['featured_image_sort'] ){
			add_filter( 'posts_fields_request', array( __CLASS__, 'posts_fields_request' ) );
			add_filter( 'posts_join_request', array( __CLASS__, 'posts_join_request' ) );
			add_filter( 'posts_orderby_request', array( __CLASS__, 'posts_orderby_request' ) );
		}
		return $query;
	}

	public static function posts_fields_request( $select ){
		global $wpdb;
		return "{$select}, IF ( _has_featured_image.meta_value IS NULL, 0, 1 ) AS has_featured_image ";
	}
	public static function posts_join_request( $join ){
		global $wpdb;
		return "{$join}\tLEFT JOIN {$wpdb->postmeta} AS _has_featured_image ON _has_featured_image.post_id = {$wpdb->posts}.ID and _has_featured_image.meta_key = '_thumbnail_id'";
	}
	public static function posts_orderby_request( $orderby ){
		return "has_featured_image DESC, {$orderby}";
	}
}
Featured_Image_Sort::init();

Using the new WP_Query argument

I like to keep things flexible, so if you want to add this to every query I would use a hook to pre_get_posts higher than a priority 10 to set the featured_image_sort flag. This is also an easy way to quickly test your code.

add_filter( 'pre_get_posts', 'sort_featured', 1 );
function sort_featured( $query ){
	$query->query_vars['featured_image_sort'] = true;
	return $query;
}

If you want to apply the sort to specific queries you would pass this flag as an argument to WP_Query as you would any other argument.

$args = array( 
	'orderby' => 'meta_value', 
	'meta_key' => 'some_meta_key', 
	'featured_image_sort' => true 
);
$query = new WP_Query( $args );

You may also like...

3 Responses

  1. Kevin says:

    Worked great for me as well. Thank you

  2. Edwin says:

    You’re a Life Saver…
    This Code WOrks like a Charm … Amazing. :3

Leave a Reply

Your email address will not be published. Required fields are marked *