让WordPress只搜索标题,优化搜索结果

本文由清尘发表于2015-03-26 14:07最后修改于2019-04-21属于WordPress分类

打开wp-includes文件夹下的query.php文件
找到以下代码:

$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";

修改为以下代码即可:

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

===========

新版本的wordpress修改以下文件:

支持修改REST API 文章搜索接口为只搜索标题

wp-json/wp/v2/posts?search='key'

查找 wp-includes/class-wp-query.php

找到如下所示

......
$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
$searchand = ' AND ';
......

修改为:

$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s))", $like);
$searchand = ' AND ';