| 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 | |