@@ -425,6 +425,13 @@ class WP_Query {
425425 */
426426 public $ thumbnails_cached = false ;
427427
428+ /**
429+ * Controls whether an attachment query should include filenames or not.
430+ *
431+ * @since 6.0.3
432+ * @var bool
433+ */
434+ protected $ allow_query_attachment_by_filename = false ;
428435 /**
429436 * Cached list of search stopwords.
430437 *
@@ -1352,8 +1359,13 @@ protected function parse_search( &$q ) {
13521359 $ q ['search_orderby_title ' ][] = $ wpdb ->prepare ( "{$ wpdb ->posts }.post_title LIKE %s " , $ like );
13531360 }
13541361
1355- $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1356- $ 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 );
1362+ $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1363+
1364+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
1365+ $ 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 );
1366+ } else {
1367+ $ 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 );
1368+ }
13571369 $ searchand = ' AND ' ;
13581370 }
13591371
@@ -1736,6 +1748,16 @@ public function get_posts() {
17361748 // Fill again in case pre_get_posts unset some vars.
17371749 $ q = $ this ->fill_query_vars ( $ q );
17381750
1751+ /**
1752+ * Filters whether an attachment query should include filenames or not.
1753+ *
1754+ * @since 6.0.3
1755+ *
1756+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1757+ */
1758+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1759+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1760+
17391761 // Parse meta query
17401762 $ this ->meta_query = new WP_Meta_Query ();
17411763 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2166,7 +2188,7 @@ public function get_posts() {
21662188 }
21672189 }
21682190
2169- if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) ) {
2191+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
21702192 $ groupby = "{$ wpdb ->posts }.ID " ;
21712193 }
21722194
@@ -2243,6 +2265,10 @@ public function get_posts() {
22432265 }
22442266 $ where .= $ search . $ whichauthor . $ whichmimetype ;
22452267
2268+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2269+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2270+ }
2271+
22462272 if ( ! empty ( $ this ->meta_query ->queries ) ) {
22472273 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
22482274 $ join .= $ clauses ['join ' ];
0 commit comments