@@ -433,6 +433,13 @@ class WP_Query {
433433 */
434434 public $ thumbnails_cached = false ;
435435
436+ /**
437+ * Controls whether an attachment query should include filenames or not.
438+ *
439+ * @since 6.0.3
440+ * @var bool
441+ */
442+ protected $ allow_query_attachment_by_filename = false ;
436443 /**
437444 * Cached list of search stopwords.
438445 *
@@ -1373,8 +1380,13 @@ protected function parse_search( &$q ) {
13731380 $ q ['search_orderby_title ' ][] = $ wpdb ->prepare ( "{$ wpdb ->posts }.post_title LIKE %s " , $ like );
13741381 }
13751382
1376- $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1377- $ search .= $ wpdb ->prepare ( "{$ searchand }(( {$ wpdb ->posts }.post_title $ like_op %s) $ andor_op ( {$ wpdb ->posts }.post_excerpt $ like_op %s) $ andor_op ( {$ wpdb ->posts }.post_content $ like_op %s)) " , $ like , $ like , $ like );
1383+ $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1384+
1385+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
1386+ $ search .= $ wpdb ->prepare ( "{$ searchand }(( {$ wpdb ->posts }.post_title $ like_op %s) $ andor_op ( {$ wpdb ->posts }.post_excerpt $ like_op %s) $ andor_op ( {$ wpdb ->posts }.post_content $ like_op %s) $ andor_op (sq1.meta_value $ like_op %s)) " , $ like , $ like , $ like , $ like );
1387+ } else {
1388+ $ search .= $ wpdb ->prepare ( "{$ searchand }(( {$ wpdb ->posts }.post_title $ like_op %s) $ andor_op ( {$ wpdb ->posts }.post_excerpt $ like_op %s) $ andor_op ( {$ wpdb ->posts }.post_content $ like_op %s)) " , $ like , $ like , $ like );
1389+ }
13781390 $ searchand = ' AND ' ;
13791391 }
13801392
@@ -1758,6 +1770,16 @@ public function get_posts() {
17581770 // Fill again in case pre_get_posts unset some vars.
17591771 $ q = $ this ->fill_query_vars ( $ q );
17601772
1773+ /**
1774+ * Filters whether an attachment query should include filenames or not.
1775+ *
1776+ * @since 6.0.3
1777+ *
1778+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1779+ */
1780+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1781+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1782+
17611783 // Parse meta query
17621784 $ this ->meta_query = new WP_Meta_Query ();
17631785 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2189,7 +2211,7 @@ public function get_posts() {
21892211 }
21902212 }
21912213
2192- if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) ) {
2214+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
21932215 $ groupby = "{$ wpdb ->posts }.ID " ;
21942216 }
21952217
@@ -2266,6 +2288,10 @@ public function get_posts() {
22662288 }
22672289 $ where .= $ search . $ whichauthor . $ whichmimetype ;
22682290
2291+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2292+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2293+ }
2294+
22692295 if ( ! empty ( $ this ->meta_query ->queries ) ) {
22702296 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
22712297 $ join .= $ clauses ['join ' ];
0 commit comments