+add_filter( 'query_vars', 'thsize_query_vars' );
+function thsize_query_vars( $vars ) {
+add_filter( 'posts_join', 'thsize_join_clauses' );
+function thsize_join_clauses( $join ) {
+ if ( isset( $q->query_vars['thsize'] ) ) {
+ $join .= " INNER JOIN {$wpdb->postmeta} as th_id ON {$wpdb->posts}.ID = th_id.post_id";
+ $join .= " INNER JOIN {$wpdb->postmeta} as th_meta ON th_meta.post_id = th_id.meta_value";
+add_filter( 'posts_where', 'thsize_where_clauses', 10, 2 );
+function thsize_where_clauses( $where, $q ) {
+ if ( isset( $q->query_vars['thsize'] ) ) {
+ $th_size = $q->query_vars['thsize'];
+ $th_size_serialized = serialize( $th_size );
+ $where .= " AND th_id.meta_key = '_thumbnail_id';";
+ $where .= " AND th_meta.meta_key = '_wp_attachment_metadata'";
+ $where .= " AND th_meta.meta_value LIKE '%{$th_size_serialized}%'";