为WP的搜索添加搜索方式

wp默认的搜索方式,按照标题和内容一并进行的.这样得到的结果太多了,很难找到自己想要的结果.

所以我把wp的搜索修改了下.

在模版页面添加了搜索方式的选择的select.

然后在includes/query.php中进行修改.
#1853行处.

foreach( (array) $q['search_terms'] as $term ) {
$term = addslashes_gpc($term);
if($_GET['where'] == ‘Title’){
$search .= “{$searchand}($wpdb->posts.post_title LIKE ‘{$n}{$term}{$n}’)”;
}
else if($_GET['where'] == ‘Content’){
$search .= “{$searchand}($wpdb->posts.post_content LIKE ‘{$n}{$term}{$n}’)”;
}
else {
$search .= “{$searchand}(($wpdb->posts.post_title LIKE ‘{$n}{$term}{$n}’) OR ($wpdb->posts.post_content LIKE ‘{$n}{$term}{$n}’))”;
}
//$searchand = ‘ AND ‘;
$searchand = ‘ OR ‘;
}
$term = esc_sql($q['s']);
//if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
// $search .= ” OR ($wpdb->posts.post_title LIKE ‘{$n}{$term}{$n}’) OR ($wpdb->posts.post_content LIKE ‘{$n}{$term}{$n}’)”;

这样就ok了.

发表评论

返回顶部