Ticket #22744: 22744.8.diff
File 22744.8.diff, 4.6 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-query.php
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']; -
tests/phpunit/tests/query/search.php
diff --git tests/phpunit/tests/query/search.php tests/phpunit/tests/query/search.php index a228f91..31392d4 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_include_file_names_in_attachment_search_as_string() { 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-image1.png', true ); 296 297 // Pass post_type a string value. 298 $q = new WP_Query( array( 299 's' => 'image1', 300 'fields' => 'ids', 301 'post_type' => 'attachment', 302 'post_status' => 'inherit', 303 ) ); 304 305 $this->assertSame( array( $attachment ), $q->posts ); 306 } 307 308 /** 309 * @ticket 22744 310 */ 311 public function test_s_should_include_file_names_in_attachment_search_as_array() { 312 $attachment = self::factory()->post->create( array( 313 'post_type' => 'attachment', 314 'post_status' => 'publish', 315 'post_title' => 'bar foo', 316 'post_content' => 'foo bar', 317 'post_excerpt' => 'This post has foo', 318 ) ); 319 320 add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true ); 321 322 // Pass post_type an array value. 323 $q = new WP_Query( array( 324 's' => 'image2', 325 'fields' => 'ids', 326 'post_type' => array( 'attachment' ), 327 'post_status' => 'inherit', 328 ) ); 329 330 $this->assertSame( array( $attachment ), $q->posts ); 331 } 332 333 /** 334 * @ticket 22744 335 */ 336 public function test_s_should_exclude_attachment_file_names_in_general_searches() { 337 $attachment = self::factory()->post->create( array( 338 'post_type' => 'attachment', 339 'post_status' => 'publish', 340 'post_title' => 'bar foo', 341 'post_content' => 'foo bar', 342 'post_excerpt' => 'This post has foo', 343 ) ); 344 345 add_post_meta( $attachment, '_wp_attached_file', 'some-image3.png', true ); 346 347 $q = new WP_Query( array( 348 's' => 'image3', 349 'fields' => 'ids', 350 'post_type' => array( 'post', 'page', 'attachment' ), 351 'post_status' => 'inherit', 352 ) ); 353 354 $this->assertNotEquals( array( $attachment ), $q->posts ); 355 } 356 283 357 public function filter_posts_search( $sql ) { 284 358 return $sql . ' /* posts_search */'; 285 359 }