@@ -486,6 +486,13 @@ class WP_Query {
486486
487487 private $ compat_methods = array ( 'init_query_flags ' , 'parse_tax_query ' );
488488
489+ /**
490+ * Controls whether an attachment query should include filenames or not.
491+ *
492+ * @since 6.0.3
493+ * @var bool
494+ */
495+ protected $ allow_query_attachment_by_filename = false ;
489496 /**
490497 * Resets query flags to false.
491498 *
@@ -1344,7 +1351,12 @@ protected function parse_search( &$q ) {
13441351 }
13451352
13461353 $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1347- $ 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 );
1354+
1355+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
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) $ andor_op (sq1.meta_value $ like_op %s)) " , $ like , $ like , $ like , $ like );
1357+ } else {
1358+ $ 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 );
1359+ }
13481360 $ searchand = ' AND ' ;
13491361 }
13501362
@@ -1681,6 +1693,16 @@ public function get_posts() {
16811693 // Fill again in case pre_get_posts unset some vars.
16821694 $ q = $ this ->fill_query_vars ($ q );
16831695
1696+ /**
1697+ * Filters whether an attachment query should include filenames or not.
1698+ *
1699+ * @since 6.0.3
1700+ *
1701+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1702+ */
1703+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1704+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1705+
16841706 // Parse meta query
16851707 $ this ->meta_query = new WP_Meta_Query ();
16861708 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2077,7 +2099,7 @@ public function get_posts() {
20772099 }
20782100 }
20792101
2080- if ( !empty ( $ this ->tax_query ->queries ) || !empty ( $ this ->meta_query ->queries ) ) {
2102+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
20812103 $ groupby = "{$ wpdb ->posts }.ID " ;
20822104 }
20832105
@@ -2126,6 +2148,10 @@ public function get_posts() {
21262148 }
21272149 $ where .= $ search . $ whichauthor . $ whichmimetype ;
21282150
2151+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2152+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2153+ }
2154+
21292155 if ( ! empty ( $ this ->meta_query ->queries ) ) {
21302156 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
21312157 $ join .= $ clauses ['join ' ];
0 commit comments