Make WordPress Core

Ticket #7394: 7394-2.patch

File 7394-2.patch, 2.1 KB (added by azaozz, 13 years ago)
  • wp-includes/query.php

     
    21852185                                preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
    21862186                                $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
    21872187                        }
     2188                        $q['search_terms'] = array_filter( (array) $q['search_terms'] );
     2189
    21882190                        $n = !empty($q['exact']) ? '' : '%';
    21892191                        $searchand = '';
    2190                         foreach( (array) $q['search_terms'] as $term ) {
     2192                        foreach ( $q['search_terms'] as $term ) {
    21912193                                $term = esc_sql( like_escape( $term ) );
    21922194                                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    21932195                                $searchand = ' AND ';
     
    23772379                                $orderby .= " {$q['order']}";
    23782380                }
    23792381
     2382                // Order search results by relevance
     2383                if ( !empty( $q['search_terms'] ) && empty( $q['exact'] ) ) {
     2384                        $orderby_search = '(CASE ';
     2385
     2386                        if ( count($q['search_terms']) > 1 ) {
     2387                                $orderby_search_s = esc_sql( like_escape( $q['s'] ) );
     2388                                $orderby_search .= "WHEN $wpdb->posts.post_title LIKE '%$orderby_search_s%' THEN 1 ";
     2389                                $orderby_search .= "WHEN $wpdb->posts.post_content LIKE '%$orderby_search_s%' THEN 2 ";
     2390                        }
     2391
     2392                        $orderby_search_title = $orderby_search_content = array();
     2393
     2394                        foreach ( $q['search_terms'] as $term ) {
     2395                                $term = esc_sql( like_escape( $term ) );
     2396                                $orderby_search_title[] = "$wpdb->posts.post_title LIKE '%$term%'";
     2397                                $orderby_search_content[] = "$wpdb->posts.post_content LIKE '%$term%'";
     2398                        }
     2399
     2400                        $orderby_search .= 'WHEN ' . implode( ' AND ', $orderby_search_title ) . ' THEN 3 ';
     2401                        $orderby_search .= 'WHEN ' . implode( ' AND ', $orderby_search_content ) . ' THEN 4 ';
     2402
     2403                        if ( count($q['search_terms']) > 1 )
     2404                                $orderby_search .= 'WHEN ' . implode( ' OR ', $orderby_search_title ) . ' THEN 5 ';
     2405
     2406                        $orderby_search .= 'ELSE 6 END), ';
     2407
     2408                        $orderby = $orderby_search . $orderby;
     2409                }
     2410
    23802411                if ( is_array( $post_type ) ) {
    23812412                        $post_type_cap = 'multiple_post_type';
    23822413                } else {