diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php
index 9590406..03c9ff2 100644
|
|
|
class WP_Query { |
| 1341 | 1341 | } |
| 1342 | 1342 | |
| 1343 | 1343 | $like = $n . $this->db->esc_like( $term ) . $n; |
| 1344 | | $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s))", $like, $like, $like ); |
| | 1344 | |
| | 1345 | // If this is a search for attachments, inlcude the filename in the search. |
| | 1346 | if ( array( 'attachment' ) === (array) $q['post_type'] ) { |
| | 1347 | $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s) $andor_op ({$this->db->postmeta}.meta_key = '_wp_attached_file' AND {$this->db->postmeta}.meta_value $like_op %s))", $like, $like, $like, $like ); |
| | 1348 | } else { |
| | 1349 | $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s))", $like, $like, $like ); |
| | 1350 | } |
| | 1351 | |
| 1345 | 1352 | $searchand = ' AND '; |
| 1346 | 1353 | } |
| 1347 | 1354 | |
| … |
… |
class WP_Query { |
| 2068 | 2075 | } |
| 2069 | 2076 | } |
| 2070 | 2077 | |
| 2071 | | if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { |
| | 2078 | if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ( $this->is_search && array( 'attachment' ) === (array) $q['post_type'] ) ) { |
| 2072 | 2079 | $groupby = "{$this->db->posts}.ID"; |
| 2073 | 2080 | } |
| 2074 | 2081 | |
| … |
… |
class WP_Query { |
| 2117 | 2124 | } |
| 2118 | 2125 | $where .= $search . $whichauthor . $whichmimetype; |
| 2119 | 2126 | |
| | 2127 | // Modify the JOIN clause for attachment searches. |
| | 2128 | if ( $this->is_search && array( 'attachment' ) === (array) $q['post_type'] ) { |
| | 2129 | $join .= " INNER JOIN {$this->db->postmeta} ON ( {$this->db->posts}.ID = {$this->db->postmeta}.post_id )"; |
| | 2130 | } |
| | 2131 | |
| 2120 | 2132 | if ( ! empty( $this->meta_query->queries ) ) { |
| 2121 | 2133 | $clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this ); |
| 2122 | 2134 | $join .= $clauses['join']; |
diff --git tests/phpunit/tests/query/search.php tests/phpunit/tests/query/search.php
index a228f91..15d2193 100644
|
|
|
class Tests_Query_Search extends WP_UnitTestCase { |
| 280 | 280 | $this->assertSame( array( $p1, $p3, $p2 ), $q->posts ); |
| 281 | 281 | } |
| 282 | 282 | |
| | 283 | /** |
| | 284 | * @ticket 22744 |
| | 285 | */ |
| | 286 | public function test_s_should_respect_attachment_file_names() { |
| | 287 | $attachment = self::factory()->post->create( array( |
| | 288 | 'post_type' => 'attachment', |
| | 289 | 'post_status' => 'publish', |
| | 290 | 'post_title' => 'bar foo', |
| | 291 | 'post_content' => 'foo bar', |
| | 292 | 'post_excerpt' => 'This post has foo', |
| | 293 | ) ); |
| | 294 | |
| | 295 | add_post_meta( $attachment, '_wp_attached_file', 'some-image.png', true ); |
| | 296 | |
| | 297 | $q = new WP_Query( array( |
| | 298 | 's' => 'image', |
| | 299 | 'fields' => 'ids', |
| | 300 | 'post_type' => array( 'attachment' ), |
| | 301 | 'post_status' => 'inherit', |
| | 302 | ) ); |
| | 303 | |
| | 304 | $this->assertSame( array( $attachment ), $q->posts ); |
| | 305 | } |
| | 306 | |
| 283 | 307 | public function filter_posts_search( $sql ) { |
| 284 | 308 | return $sql . ' /* posts_search */'; |
| 285 | 309 | } |