@@ -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 *
@@ -1391,7 +1398,12 @@ protected function parse_search( &$q ) {
13911398 }
13921399
13931400 $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1394- $ 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 );
1401+
1402+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
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) $ andor_op (sq1.meta_value $ like_op %s)) " , $ like , $ like , $ like , $ like );
1404+ } else {
1405+ $ 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 );
1406+ }
13951407 $ searchand = ' AND ' ;
13961408 }
13971409
@@ -1784,6 +1796,16 @@ public function get_posts() {
17841796 // Fill again in case 'pre_get_posts' unset some vars.
17851797 $ q = $ this ->fill_query_vars ( $ q );
17861798
1799+ /**
1800+ * Filters whether an attachment query should include filenames or not.
1801+ *
1802+ * @since 6.0.3
1803+ *
1804+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1805+ */
1806+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1807+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1808+
17871809 // Parse meta query.
17881810 $ this ->meta_query = new WP_Meta_Query ();
17891811 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2215,7 +2237,7 @@ public function get_posts() {
22152237 }
22162238 }
22172239
2218- if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) ) {
2240+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
22192241 $ groupby = "{$ wpdb ->posts }.ID " ;
22202242 }
22212243
@@ -2292,6 +2314,10 @@ public function get_posts() {
22922314 }
22932315 $ where .= $ search . $ whichauthor . $ whichmimetype ;
22942316
2317+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2318+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2319+ }
2320+
22952321 if ( ! empty ( $ this ->meta_query ->queries ) ) {
22962322 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
22972323 $ join .= $ clauses ['join ' ];
0 commit comments