@@ -441,6 +441,13 @@ class WP_Query {
441441 */
442442 public $ thumbnails_cached = false ;
443443
444+ /**
445+ * Controls whether an attachment query should include filenames or not.
446+ *
447+ * @since 6.0.3
448+ * @var bool
449+ */
450+ protected $ allow_query_attachment_by_filename = false ;
444451 /**
445452 * Cached list of search stopwords.
446453 *
@@ -1388,8 +1395,13 @@ protected function parse_search( &$q ) {
13881395 $ q ['search_orderby_title ' ][] = $ wpdb ->prepare ( "{$ wpdb ->posts }.post_title LIKE %s " , $ like );
13891396 }
13901397
1391- $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1392- $ 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 );
1398+ $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1399+
1400+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
1401+ $ 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 );
1402+ } else {
1403+ $ 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 );
1404+ }
13931405 $ searchand = ' AND ' ;
13941406 }
13951407
@@ -1773,6 +1785,16 @@ public function get_posts() {
17731785 // Fill again in case 'pre_get_posts' unset some vars.
17741786 $ q = $ this ->fill_query_vars ( $ q );
17751787
1788+ /**
1789+ * Filters whether an attachment query should include filenames or not.
1790+ *
1791+ * @since 6.0.3
1792+ *
1793+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1794+ */
1795+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1796+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1797+
17761798 // Parse meta query.
17771799 $ this ->meta_query = new WP_Meta_Query ();
17781800 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2204,7 +2226,7 @@ public function get_posts() {
22042226 }
22052227 }
22062228
2207- if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) ) {
2229+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
22082230 $ groupby = "{$ wpdb ->posts }.ID " ;
22092231 }
22102232
@@ -2281,6 +2303,10 @@ public function get_posts() {
22812303 }
22822304 $ where .= $ search . $ whichauthor . $ whichmimetype ;
22832305
2306+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2307+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2308+ }
2309+
22842310 if ( ! empty ( $ this ->meta_query ->queries ) ) {
22852311 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
22862312 $ join .= $ clauses ['join ' ];
0 commit comments