WordPress默认的搜索功能是只要文章出现的词搜索的时候一起匹配,虽然这种搜索方式很全面,但是这种一般会得到很多不正确的结果,会增加信息获取的难度,那么如何使用WordPress代码实现只搜索文章标题?如果不想匹配文章内容中出现的关键词,我们可以通过posts_search过滤器,将搜索结果替换成我们自己的搜索逻辑即可。

操作步骤:

新增配置项

在主题设置配置文件options.php中添加如下配置项,位置随便你,建议添加到基本选项卡中。

$options[] = array(

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => __(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’搜索时关键字只匹配标题\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’haoui\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’),

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’search_title\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’type\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”checkbox\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’std\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => true,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’desc\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => __(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’开启\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’haoui\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’));

替换搜索逻辑

以下代码放在functions.php中,或者被functions.php引用的文件中。

function hui_search_by_title_only( $search ){

global $wpdb, $wp_query;

if ( empty( $search ) )

return $search;

$q = $wp_query->query_vars;

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

$search =

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

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

$term = esc_sql( $wpdb->esc_like( $term ) );

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

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

}

if ( ! empty( $search ) ) {

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

if ( ! is_user_logged_in() )

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

}

return $search;

}

使用主题配置项控制,请在上面代码结束后添加如下代码

if( _hui(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’search_title\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) ){

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

}

如果你不想要后台选项,可以将上面代码替换掉。

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

相关新闻

发表评论

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

联系我们

联系我们

151-1101-0018

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

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

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