Ticket #20044: 20044.3.diff
| File 20044.3.diff, 2.6 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/query.php
1958 1958 } 1959 1959 } 1960 1960 1961 $fields = apply_filters( 'query_search_fields', array( "$wpdb->posts.post_title", "$wpdb->posts.post_content" ) ); 1962 $or = array(); 1963 foreach ( (array) $fields as $field ) { 1964 $or[] = '(' . $field . ' LIKE \'%1$s\')'; 1965 } 1966 $conditions = join( ' OR ', $or ); 1967 1961 1968 $n = ! empty( $q['exact'] ) ? '' : '%'; 1962 1969 $searchand = ''; 1963 1970 $q['search_orderby_title'] = array(); 1964 1971 foreach ( $q['search_terms'] as $term ) { 1965 1972 $term = like_escape( esc_sql( $term ) ); 1966 if ( $n ) 1973 if ( $n ) { 1967 1974 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'"; 1968 1969 $search .= "{$searchand}( ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";1975 } 1976 $search .= "{$searchand}(" . sprintf( $conditions, "{$n}{$term}{$n}" ) . ")"; 1970 1977 $searchand = ' AND '; 1971 1978 } 1972 1979 … … 1975 1982 if ( ! is_user_logged_in() ) 1976 1983 $search .= " AND ($wpdb->posts.post_password = '') "; 1977 1984 } 1978 1979 1985 return $search; 1980 1986 } 1981 1987 -
tests/phpunit/tests/query/search.php
1 <?php 2 /** 3 * Search functionality 4 * 5 * @group query 6 * @group search 7 */ 8 class Tests_Query_Search extends WP_UnitTestCase { 9 10 function test_search_by_fields() { 11 $post_id = $this->factory->post->create( array( 'post_title' => 'Taco', 'post_name' => 'burrito', 'post_content' => 'Enchilada' ) ); 12 13 // post title 14 $q1 = new WP_Query( array( 's' => 'taco', 'fields' => 'ids' ) ); 15 $this->assertEquals( array( $post_id ), $q1->posts ); 16 17 // post content 18 $q2 = new WP_Query( array( 's' => 'enchilada', 'fields' => 'ids' ) ); 19 $this->assertEquals( array( $post_id ), $q2->posts ); 20 21 // post slug 22 $q3 = new WP_Query( array( 's' => 'Burrito', 'fields' => 'ids' ) ); 23 $this->assertEquals( array(), $q3->posts ); 24 25 add_filter( 'query_search_fields', array( $this, 'filter_query_search_fields' ) ); 26 $q4 = new WP_Query( array( 's' => 'Burrito', 'fields' => 'ids' ) ); 27 $this->assertEquals( array( $post_id ), $q4->posts ); 28 remove_filter( 'query_search_fields', array( $this, 'filter_query_search_fields' ) ); 29 } 30 31 function filter_query_search_fields( $fields ) { 32 global $wpdb; 33 $fields[] = "{$wpdb->posts}.post_name"; 34 return $fields; 35 } 36 } 37 No newline at end of file