@@ -444,6 +444,13 @@ class WP_Query {
444444 */
445445 public $ thumbnails_cached = false ;
446446
447+ /**
448+ * Controls whether an attachment query should include filenames or not.
449+ *
450+ * @since 6.0.3
451+ * @var bool
452+ */
453+ protected $ allow_query_attachment_by_filename = false ;
447454 /**
448455 * Cached list of search stopwords.
449456 *
@@ -1394,7 +1401,12 @@ protected function parse_search( &$q ) {
13941401 }
13951402
13961403 $ like = $ n . $ wpdb ->esc_like ( $ term ) . $ n ;
1397- $ 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+
1405+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
1406+ $ 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 );
1407+ } else {
1408+ $ 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 );
1409+ }
13981410 $ searchand = ' AND ' ;
13991411 }
14001412
@@ -1789,6 +1801,16 @@ public function get_posts() {
17891801 // Fill again in case 'pre_get_posts' unset some vars.
17901802 $ q = $ this ->fill_query_vars ( $ q );
17911803
1804+ /**
1805+ * Filters whether an attachment query should include filenames or not.
1806+ *
1807+ * @since 6.0.3
1808+ *
1809+ * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1810+ */
1811+ $ this ->allow_query_attachment_by_filename = apply_filters ( 'wp_allow_query_attachment_by_filename ' , false );
1812+ remove_all_filters ( 'wp_allow_query_attachment_by_filename ' );
1813+
17921814 // Parse meta query.
17931815 $ this ->meta_query = new WP_Meta_Query ();
17941816 $ this ->meta_query ->parse_query_vars ( $ q );
@@ -2220,7 +2242,7 @@ public function get_posts() {
22202242 }
22212243 }
22222244
2223- if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) ) {
2245+ if ( ! empty ( $ this ->tax_query ->queries ) || ! empty ( $ this ->meta_query ->queries ) || ! empty ( $ this -> allow_query_attachment_by_filename ) ) {
22242246 $ groupby = "{$ wpdb ->posts }.ID " ;
22252247 }
22262248
@@ -2297,6 +2319,10 @@ public function get_posts() {
22972319 }
22982320 $ where .= $ search . $ whichauthor . $ whichmimetype ;
22992321
2322+ if ( ! empty ( $ this ->allow_query_attachment_by_filename ) ) {
2323+ $ join .= " LEFT JOIN {$ wpdb ->postmeta } AS sq1 ON ( {$ wpdb ->posts }.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' ) " ;
2324+ }
2325+
23002326 if ( ! empty ( $ this ->meta_query ->queries ) ) {
23012327 $ clauses = $ this ->meta_query ->get_sql ( 'post ' , $ wpdb ->posts , 'ID ' , $ this );
23022328 $ join .= $ clauses ['join ' ];
0 commit comments