Make WordPress Core

Ticket #20044: 20044.4.diff

File 20044.4.diff, 3.3 KB (added by jeremyfelt, 12 years ago)
  • src/wp-includes/query.php

     
    19731973                        }
    19741974                }
    19751975
     1976                /**
     1977                 * Filter the fields used when searching the posts table.
     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_content, post_title, post_excerpt, and post_name.
     1984                 *
     1985                 * @since 3.9.0
     1986                 *
     1987                 * @param array $fields Fields from a list of allowed search fields.
     1988                 */
     1989                $fields = apply_filters( 'query_search_fields', array( 'post_title', 'post_content' ) );
     1990                $allowed_search_fields = array( 'post_content', 'post_title', 'post_excerpt', 'post_name' );
     1991                $or = array();
     1992                foreach( (array) $fields as $field ) {
     1993                        if ( in_array( $field, $allowed_search_fields ) ) {
     1994                                $or[] = "( $wpdb->posts.$field" . ' LIKE \'%1$s\')';
     1995                        }
     1996                }
     1997                $conditions = join( ' OR ', $or );
     1998
    19761999                $n = ! empty( $q['exact'] ) ? '' : '%';
    19772000                $searchand = '';
    19782001                $q['search_orderby_title'] = array();
    19792002                foreach ( $q['search_terms'] as $term ) {
    19802003                        $term = like_escape( esc_sql( $term ) );
    1981                         if ( $n )
     2004                        if ( $n ) {
    19822005                                $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}'))";
     2006                        }
     2007                        $search .= "{$searchand}(" . sprintf( $conditions, "{$n}{$term}{$n}" ) . ")";
    19852008                        $searchand = ' AND ';
    19862009                }
    19872010
  • tests/phpunit/tests/query/search.php

     
     1<?php
     2/**
     3 * Search functionality
     4 *
     5 * @group query
     6 * @group search
     7 */
     8class 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', 'post_excerpt' => 'Torta' ) );
     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                // post excerpt
     26                $q4 = new WP_Query( array( 's' => 'Torta', 'fields' => 'ids' ) );
     27                $this->assertEquals( array(), $q4->posts );
     28
     29                add_filter( 'query_search_fields', array( $this, 'filter_query_search_fields' ) );
     30                $q5 = new WP_Query( array( 's' => 'Burrito', 'fields' => 'ids' ) );
     31                $this->assertEquals( array( $post_id ), $q5->posts );
     32
     33                $q6 = new WP_Query( array( 's' => 'Torta', 'fields' => 'ids' ) );
     34                $this->assertEquals( array( $post_id ), $q6->posts );
     35                remove_filter( 'query_search_fields', array( $this, 'filter_query_search_fields' ) );
     36        }
     37
     38        function filter_query_search_fields( $fields ) {
     39                $fields[] = "post_name";
     40                $fields[] = "post_excerpt";
     41                return $fields;
     42        }
     43}
     44 No newline at end of file