@@ -437,6 +437,13 @@ class WP_Query {
437437
438438 private $ compat_methods = array ( 'init_query_flags ' , 'parse_tax_query ' );
439439
440+ /**
441+ * Controls whether an attachment query should include filenames or not.
442+ *
443+ * @since 6.0.3
444+ * @var bool
445+ */
446+ protected $ allow_query_attachment_by_filename = false ;
440447 /**
441448 * Resets query flags to false.
442449 *
@@ -1298,7 +1305,12 @@ protected function parse_search( &$q ) {
12981305 }
12991306
13001307 $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1301- $ 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 );
1308+
1309+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
1310+ $ 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 );
1311+ } else {
1312+ $ 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 );
1313+ }
13021314 $ searchand = ' AND ' ;
13031315 }
13041316
@@ -1634,6 +1646,16 @@ public function get_posts() {
16341646 // Fill again in case pre_get_posts unset some vars.
16351647 $ q = $ this ->fill_query_vars ($ q );
16361648
1649+ /**
1650+ * Filters whether an attachment query should include filenames or not.
1651+ *
1652+ * @since 6.0.3
1653+ *
1654+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1655+ */
1656+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1657+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1658+
16371659 // Parse meta query
16381660 $ this ->meta_query = new WP_Meta_Query ();
16391661 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2038,7 +2060,7 @@ public function get_posts() {
20382060 }
20392061 }
20402062
2041- if ( !empty ( $ this ->tax_query ->queries ) || !empty ( $ this ->meta_query ->queries ) ) {
2063+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
20422064 $ groupby = "{$ wpdb ->posts }.ID " ;
20432065 }
20442066
@@ -2111,6 +2133,10 @@ public function get_posts() {
21112133 }
21122134 $ where .= $ search . $ whichauthor . $ whichmimetype ;
21132135
2136+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2137+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2138+ }
2139+
21142140 if ( ! empty ( $ this ->meta_query ->queries ) ) {
21152141 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
21162142 $ join .= $ clauses ['join ' ];
0 commit comments