| 2384 | // Order search results by relevance |
| 2385 | if ( !empty( $search_orderby_title ) && empty( $q['exact'] ) ) { |
| 2386 | $num_terms = count($search_orderby_title); |
| 2387 | |
| 2388 | if ( $num_terms > 1 ) { |
| 2389 | $search_orderby_s = esc_sql( like_escape( $q['s'] ) ); |
| 2390 | |
| 2391 | $search_orderby = '(CASE '; |
| 2392 | // sentence match in title |
| 2393 | $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%$search_orderby_s%' THEN 1 "; |
| 2394 | |
| 2395 | // sanity limit |
| 2396 | if ( $num_terms < 7 ) { |
| 2397 | // all words in title |
| 2398 | $search_orderby .= 'WHEN ' . implode( ' AND ', $search_orderby_title ) . ' THEN 2 '; |
| 2399 | // any word in title |
| 2400 | $search_orderby .= 'WHEN ' . implode( ' OR ', $search_orderby_title ) . ' THEN 3 '; |
| 2401 | } |
| 2402 | |
| 2403 | // sentence match in content |
| 2404 | $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%$search_orderby_s%' THEN 4 "; |
| 2405 | $search_orderby .= 'ELSE 5 END)'; |
| 2406 | } else { |
| 2407 | // single word or sentence search |
| 2408 | $search_orderby = reset($search_orderby_title) . ' DESC'; |
| 2409 | } |
| 2410 | // Allow plugins to add/remove/modify the 'order by' for the search section of the database query |
| 2411 | $search_orderby = apply_filters_ref_array( 'posts_search_orderby', array( $search_orderby, &$this ) ); |
| 2412 | if ( $search_orderby ) |
| 2413 | $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; |
| 2414 | } |
| 2415 | |