Make WordPress Core

Ticket #20044: 20044.3.diff

File 20044.3.diff, 2.6 KB (added by wonderboymusic, 12 years ago)
  • src/wp-includes/query.php

     
    19581958                        }
    19591959                }
    19601960
     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
    19611968                $n = ! empty( $q['exact'] ) ? '' : '%';
    19621969                $searchand = '';
    19631970                $q['search_orderby_title'] = array();
    19641971                foreach ( $q['search_terms'] as $term ) {
    19651972                        $term = like_escape( esc_sql( $term ) );
    1966                         if ( $n )
     1973                        if ( $n ) {
    19671974                                $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}" ) . ")";
    19701977                        $searchand = ' AND ';
    19711978                }
    19721979
     
    19751982                        if ( ! is_user_logged_in() )
    19761983                                $search .= " AND ($wpdb->posts.post_password = '') ";
    19771984                }
    1978 
    19791985                return $search;
    19801986        }
    19811987
  • 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' ) );
     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