Ticket #20044: 20044.5.diff
| File 20044.5.diff, 4.2 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/query.php
1973 1973 } 1974 1974 } 1975 1975 1976 /** 1977 * Filter the fields used when searching for posts. 1978 * 1979 * By default, a search on the posts table will use post_title and 1980 * post_content. This filter is available to expand that search to 1981 * other applicable fields. 1982 * 1983 * Allowed fields are post_title, post_content, post_excerpt, post_name, 1984 * and post_content_filtered. 1985 * 1986 * @since 3.9.0 1987 * 1988 * @param array $fields Fields from a list of allowed search fields. 1989 * @param WP_Query $this The WP_Query instance. 1990 */ 1991 $fields = apply_filters( 'posts_search_fields', array( 'post_title', 'post_content' ), $this ); 1992 $allowed = array( 'post_title', 'post_content', 'post_excerpt', 'post_name', 'post_content_filtered' ); 1993 $fields = array_intersect( $fields, $allowed ); 1994 1995 $conditions = array(); 1996 foreach( $fields as $field ) { 1997 $conditions[] = "( $wpdb->posts.$field" . ' LIKE \'%1$s\')'; 1998 } 1999 $conditions = join( ' OR ', $conditions ); 2000 1976 2001 $n = ! empty( $q['exact'] ) ? '' : '%'; 1977 2002 $searchand = ''; 1978 2003 $q['search_orderby_title'] = array(); 1979 2004 foreach ( $q['search_terms'] as $term ) { 1980 2005 $term = like_escape( esc_sql( $term ) ); 1981 if ( $n ) 2006 if ( $n ) { 1982 2007 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'"; 1983 1984 $search .= "{$searchand}( ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";2008 } 2009 $search .= "{$searchand}(" . sprintf( $conditions, "{$n}{$term}{$n}" ) . ")"; 1985 2010 $searchand = ' AND '; 1986 2011 } 1987 2012 -
tests/phpunit/tests/query/search.php
29 29 return $this->q->query( $args ); 30 30 } 31 31 32 /** 33 * @ticket 7394 34 */ 32 35 function test_search_order_title_relevance() { 33 36 foreach ( range( 1, 7 ) as $i ) 34 37 $this->factory->post->create( array( 'post_content' => $i . rand_str() . ' about', 'post_type' => $this->post_type ) ); … … 38 41 $this->assertEquals( $post_id, reset( $posts )->ID ); 39 42 } 40 43 44 /** 45 * @ticket 7394 46 */ 41 47 function test_search_terms_query_var() { 42 48 $terms = 'This is a search term'; 43 49 $query = new WP_Query( array( 's' => 'This is a search term' ) ); … … 45 51 $this->assertEquals( array( 'search', 'term' ), $query->get( 'search_terms' ) ); 46 52 } 47 53 54 /** 55 * @ticket 7394 56 */ 48 57 function test_filter_stopwords() { 49 58 $terms = 'This is a search term'; 50 59 add_filter( 'wp_search_stopwords', array( $this, 'filter_wp_search_stopwords' ) ); … … 58 67 function filter_wp_search_stopwords() { 59 68 return array(); 60 69 } 70 71 /** 72 * @ticket 20044 73 */ 74 function test_search_by_fields() { 75 $post_id = $this->factory->post->create( array( 'post_title' => 'Taco', 'post_name' => 'burrito', 'post_content' => 'Enchilada', 'post_excerpt' => 'Torta' ) ); 76 77 // post title 78 $q1 = new WP_Query( array( 's' => 'taco', 'fields' => 'ids' ) ); 79 $this->assertEquals( array( $post_id ), $q1->posts ); 80 81 // post content 82 $q2 = new WP_Query( array( 's' => 'enchilada', 'fields' => 'ids' ) ); 83 $this->assertEquals( array( $post_id ), $q2->posts ); 84 85 // post slug 86 $q3 = new WP_Query( array( 's' => 'Burrito', 'fields' => 'ids' ) ); 87 $this->assertEquals( array(), $q3->posts ); 88 89 // post excerpt 90 $q4 = new WP_Query( array( 's' => 'Torta', 'fields' => 'ids' ) ); 91 $this->assertEquals( array(), $q4->posts ); 92 93 add_filter( 'query_search_fields', array( $this, 'filter_query_search_fields' ) ); 94 $q5 = new WP_Query( array( 's' => 'Burrito', 'fields' => 'ids' ) ); 95 $this->assertEquals( array( $post_id ), $q5->posts ); 96 97 $q6 = new WP_Query( array( 's' => 'Torta', 'fields' => 'ids' ) ); 98 $this->assertEquals( array( $post_id ), $q6->posts ); 99 remove_filter( 'query_search_fields', array( $this, 'filter_query_search_fields' ) ); 100 } 101 102 function filter_query_search_fields( $fields ) { 103 $fields[] = "post_name"; 104 $fields[] = "post_excerpt"; 105 return $fields; 106 } 61 107 }