WordpPress 自带的搜索功能其实比较简单,搜索的结果就是包含搜索词的文章、页面,按照时间发布顺序排序,那么如何增强WordPress搜索结果相关性准确度?我们一起了解一下。

1.增加关联性

if(is_search()){

add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’posts_orderby_request\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’search_orderby_filter\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

}

function 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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

}

只搜索文章

只搜索文章的标题,将下面的代码添加到主题的 functions.php 文件即可:

/**

* 让 WordPress 只搜索文章的标题

*/

function __search_by_title_only( $search, &$wp_query )

{

global $wpdb;

if ( emptyempty( $search ) )

return $search; // skip processing – no search term in query

$q = $wp_query->query_vars;

$n = ! emptyempty( $q[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’exact\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] ) ? \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ : \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’%\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

$search =

$searchand = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

foreach ( (array) $q[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’search_terms\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] as $term ) {

$term = esc_sql( like_escape( $term ) );

$search .= \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”{$searchand}($wpdb->posts.post_title LIKE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'{$n}{$term}{$n}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

$searchand = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ AND \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

}

if ( ! emptyempty( $search ) ) {

$search = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” AND ({$search}) \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

if ( ! is_user_logged_in() )

$search .= \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” AND ($wpdb->posts.post_password = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

}

return $search;

}

add_filter( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’posts_search\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’__search_by_title_only\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, 500, 2 );

选择分类搜索:

<form id=”searchform” name=”searchform” method=”get” action=”<?php bloginfo(‘home’); ?>/” >

<ul>

<li>

<p>

<?php $select = wp_dropdown_categories(‘class=search_select&show_option_all=全站搜索& amp;orderby=name&hierarchical=0&selected=-1&depth=1′);?>

</p>

</li>

<li>

<input type=”text” name=”s” id=”s” maxlength=”34″ value=””/>

</li>

<li>

<input type=”image” value=”” src=”<?php bloginfo(‘template_url’); ?>/img/search.gif”/>

</li>

</ul>

</form>

显示出的效果类似下图功能,可选择全站搜索,或者具体的分类搜索,搜索的结果更加精准!

具体样式得自己修改了。

多重选项框搜索

这种方法更加强大,可以选择多个分类,并搜索分类中的文章,精确度更好,不过使用这种方法有一定的固定性,你需要自己写好选项框中的分类id:

将你的默认的searchform.php修改为以下代码:

<div>

<form id=”index_search” name=”index_search” method=”get” action=”<?php bloginfo(‘home’); ?>/”>

<p><input type=”text” name=”s” id=”s” value=””/> <input type=”submit” value=” 搜 索 ” /></p>

<p>

<label for=”s_type5″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”all” checked>全站</label>

<label for=”s_type1″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”4″ checked>主题</label>

<label for=”s_type2″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”6″>插件</label>

<label for=”s_type3″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”3″>主机</label>

<label for=”s_type4″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”10″>经验</label>

</p>

</form>

</div>

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部