Ticket #22744: 22744.7.diff
File 22744.7.diff, 3.9 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..dc7e1dc 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 { 2117 2124 } 2118 2125 $where .= $search . $whichauthor . $whichmimetype; 2119 2126 2127 // Modify the JOIN & DISTINCT clauses 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 $distinct = 'DISTINCT'; 2131 } 2132 2120 2133 if ( ! empty( $this->meta_query->queries ) ) { 2121 2134 $clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this ); 2122 2135 $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..c50fa2a 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_searces() { 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 // Pass post_type a string value. 298 $q_string = new WP_Query( array( 299 's' => 'image', 300 'fields' => 'ids', 301 'post_type' => 'attachment', 302 'post_status' => 'inherit', 303 ) ); 304 305 // Pass post_type an array value. 306 $q_array = new WP_Query( array( 307 's' => 'image', 308 'fields' => 'ids', 309 'post_type' => array( 'attachment' ), 310 'post_status' => 'inherit', 311 ) ); 312 313 $this->assertSame( array( $attachment ), $q_string->posts ); 314 $this->assertSame( array( $attachment ), $q_array->posts ); 315 } 316 317 /** 318 * @ticket 22744 319 */ 320 public function test_s_should_exclude_attachment_file_names_in_general_searches() { 321 $attachment = self::factory()->post->create( array( 322 'post_type' => 'attachment', 323 'post_status' => 'publish', 324 'post_title' => 'bar foo', 325 'post_content' => 'foo bar', 326 'post_excerpt' => 'This post has foo', 327 ) ); 328 329 add_post_meta( $attachment, '_wp_attached_file', 'some-image.png', true ); 330 331 $q = new WP_Query( array( 332 's' => 'image', 333 'fields' => 'ids', 334 'post_type' => array( 'post', 'page', 'attachment' ), 335 'post_status' => 'inherit', 336 ) ); 337 338 $this->assertNotEquals( array( $attachment ), $q->posts ); 339 } 340 283 341 public function filter_posts_search( $sql ) { 284 342 return $sql . ' /* posts_search */'; 285 343 }