實在扛不住 google 自定義搜索的速度了,把之前的搜索換回了 wordpress 自帶的搜索,但是 wordpress 的默認搜索結果是按照文章的發(fā)布時間來排序的,這樣的搜索結果的相關性并不強,應該讓搜索結果按照內容相關性排序,而不是按照時間或者 id,所以我們可以在當前主題的 functions.php添加如下代碼來增強 wordpress 搜索的相關性:
add_filter(‘posts_orderby_request’, ‘wpjam_search_orderby_filter’);
function wpjam_search_orderby_filter($orderby = ‘’){
global $wpdb;
$keyword = $wpdb->prepare($_request[‘s’]);
return “((case when {$wpdb->posts}.post_title like ‘%{$keyword}%’ then 2 else 0 end) + (case when {$wpdb->posts}.post_content like ‘%{$keyword}%’ then 1 else 0 end)) desc, {$wpdb->posts}.post_modified desc, {$wpdb->posts}.id asc”;
}
上面的代碼就是使得搜索的結構代碼,先按照日志的標題的相關性排序,然后按照日志的內容,最后才是日的志修改時間和 id,這樣的修改之后,wordpress 搜索的結果相關性就提高了很多。