我有一个通过自定义帖子类型运行的搜索查询,但是它搜索文本的所有部分,而不是完整的句子。
例如如果您搜索Menta作为公司名称,它还将在文本内容中显示带有Comple menta ry的条目。
我需要在下面找到一种修改此功能的方法,使其仅搜索帖子标题,而不进行其他任何搜索,这将是理想的解决方案。或者,如果那不可能,则仅搜索完整单词而不是部分内容。我希望这是有道理的。
这是代码:
function aitIncludeMetaInSearch($where) {
remove_filter('posts_where', 'aitIncludeMetaInSearch');
global $wpdb;
// this filter callbeck can be used in ajax requests
// search string doesn't exist in ajax requests
// therefore search keyword must be passed as a global query from other scope
global $__ait_query_data;
$searchKeyword = $__ait_query_data['search-params']['s'];
// list of meta keys where we are searching
$metaKeys = array('subtitle', 'features_search_string');
$metaKeys = "'" . implode( "', '", esc_sql( $metaKeys ) ) . "'";
$toFind = "(".$wpdb->posts . ".post_title";
$likeClause = $wpdb->esc_like( $searchKeyword );
$likeClause = '%' . $likeClause . '%';
// search in postmeta values and return array of post ids
$subQuery = $wpdb->prepare(
"SELECT group_concat(post_id) as ids FROM {$wpdb->postmeta} AS postmeta
WHERE postmeta.meta_value LIKE %s
AND postmeta.meta_key IN ({$metaKeys})",
$likeClause
);
$subQuery = "(FIND_IN_SET(".$wpdb->posts.".ID, (".$subQuery."))) OR ";
$subqueryLength = strlen($subQuery);
$positions = aitFindStrPos($toFind, $where);
$newWhere = $where;
for ($i = 0; $i < sizeof($positions); $i++) {
// insert subquery on each position where $toFind occured
// consider that each next position is moved by the length of previously inserted subquery
$newWhere = substr_replace($newWhere, $subQuery, $positions[$i] + $i * $subqueryLength, 0);
}
// Return revised WHERE clause
return $newWhere;
}
任何帮助都会很棒:D
答案 0 :(得分:0)
此功能似乎旨在添加元搜索,而不是将现有搜索仅限于帖子标题。如果要这样做,请尝试删除已有的代码,然后在此处使用代码:
Make wordpress search only in post title
全字vs部分匹配不能通过LIKE查询来完成,而可以通过正则表达式来实现
请参阅:Search for "whole word match" in MySQL
function aitIncludeMetaInSearch($where) {
remove_filter('posts_where', 'aitIncludeMetaInSearch');
global $wpdb;
// this filter callbeck can be used in ajax requests
// search string doesn't exist in ajax requests
// therefore search keyword must be passed as a global query from other scope
global $__ait_query_data;
$searchKeyword = $__ait_query_data['search-params']['s'];
// list of meta keys where we are searching
$metaKeys = array('subtitle', 'features_search_string');
$metaKeys = "'" . implode( "', '", esc_sql( $metaKeys ) ) . "'";
$toFind = "(".$wpdb->posts . ".post_title";
$likeClause = $wpdb->esc_like( $searchKeyword );
$regexClause = '[[:<:]]' . $likeClause . '[[:>:]]';
// search in postmeta values and return array of post ids
$subQuery = $wpdb->prepare(
"SELECT group_concat(post_id) as ids FROM {$wpdb->postmeta} AS postmeta
WHERE postmeta.meta_value REGEXP %s
AND postmeta.meta_key IN ({$metaKeys})",
$regexClause
);
$subQuery = "(FIND_IN_SET(".$wpdb->posts.".ID, (".$subQuery."))) OR ";
$subqueryLength = strlen($subQuery);
$positions = aitFindStrPos($toFind, $where);
$newWhere = $where;
for ($i = 0; $i < sizeof($positions); $i++) {
// insert subquery on each position where $toFind occured
// consider that each next position is moved by the length of previously inserted subquery
$newWhere = substr_replace($newWhere, $subQuery, $positions[$i] + $i * $subqueryLength, 0);
}
// Return revised WHERE clause
return $newWhere;
}