Ticket #7394: basic.7394.diff

File basic.7394.diff, 1.4 KB (added by scribu, 2 years ago)
Line 
1Index: wp-includes/query.php
2===================================================================
3--- wp-includes/query.php       (revision 17607)
4+++ wp-includes/query.php       (working copy)
5@@ -2148,9 +2148,11 @@
6                                preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
7                                $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
8                        }
9+                       $q['search_terms'] = array_filter( (array) $q['search_terms'] );
10+
11                        $n = !empty($q['exact']) ? '' : '%';
12                        $searchand = '';
13-                       foreach( (array) $q['search_terms'] as $term ) {
14+                       foreach ( $q['search_terms'] as $term ) {
15                                $term = esc_sql( like_escape( $term ) );
16                                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
17                                $searchand = ' AND ';
18@@ -2345,7 +2347,19 @@
19                        if ( empty($q['orderby']) )
20                                $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
21                }
22+               
23+               // Order search results by relevance
24+               if ( !empty( $q['search_terms'] ) && empty( $q['exact'] ) ) {
25+                       $orderby_search = array();
26+                       foreach ( $q['search_terms'] as $term ) {
27+                               $term = esc_sql( like_escape( $term ) );
28+                               $orderby_search[] = "$wpdb->posts.post_title LIKE '%$term%'";
29+                       }
30+                       $orderby_search = implode( ' OR ', $orderby_search );
31 
32+                       $q['orderby'] = "($orderby_search) DESC, " . $q['orderby'];
33+               }
34+
35                if ( is_array( $post_type ) ) {
36                        $post_type_cap = 'multiple_post_type';
37                } else {