Make WordPress Core

Ticket #20044: 20044.5.diff

File 20044.5.diff, 4.2 KB (added by nacin, 12 years ago)
  • src/wp-includes/query.php

     
    19731973                        }
    19741974                }
    19751975
     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
    19762001                $n = ! empty( $q['exact'] ) ? '' : '%';
    19772002                $searchand = '';
    19782003                $q['search_orderby_title'] = array();
    19792004                foreach ( $q['search_terms'] as $term ) {
    19802005                        $term = like_escape( esc_sql( $term ) );
    1981                         if ( $n )
     2006                        if ( $n ) {
    19822007                                $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}" ) . ")";
    19852010                        $searchand = ' AND ';
    19862011                }
    19872012
  • tests/phpunit/tests/query/search.php

     
    2929                return $this->q->query( $args );
    3030        }
    3131
     32        /**
     33         * @ticket 7394
     34         */
    3235        function test_search_order_title_relevance() {
    3336                foreach ( range( 1, 7 ) as $i )
    3437                        $this->factory->post->create( array( 'post_content' => $i . rand_str() . ' about', 'post_type' => $this->post_type ) );
     
    3841                $this->assertEquals( $post_id, reset( $posts )->ID );
    3942        }
    4043
     44        /**
     45         * @ticket 7394
     46         */
    4147        function test_search_terms_query_var() {
    4248                $terms = 'This is a search term';
    4349                $query = new WP_Query( array( 's' => 'This is a search term' ) );
     
    4551                $this->assertEquals( array( 'search', 'term' ), $query->get( 'search_terms' ) );
    4652        }
    4753
     54        /**
     55         * @ticket 7394
     56         */
    4857        function test_filter_stopwords() {
    4958                $terms = 'This is a search term';
    5059                add_filter( 'wp_search_stopwords', array( $this, 'filter_wp_search_stopwords' ) );
     
    5867        function filter_wp_search_stopwords() {
    5968                return array();
    6069        }
     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        }
    61107}