更改搜索查询 WordPress



我有一个通过自定义帖子类型运行的搜索查询,但它搜索的是文本的所有部分,而不是完整的句子。

例如,如果您搜索 Menta 作为公司名称,它还会在文本内容中显示一个带有 Complementary 的条目。

我需要找到一种方法来修改下面的此功能,以仅搜索帖子标题而不搜索其他任何内容,这将是理想的解决方案。或者,如果这是不可能的,那么只搜索完整的单词而不是部分。我希望这是有道理的。

这是代码:

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

这个函数看起来旨在添加元搜索,而不是将现有搜索限制为仅对帖子标题。如果要执行此操作,请尝试删除您拥有的代码并使用此处的代码:

仅在帖子标题中进行wordpress搜索

完整的单词与部分匹配不能用 LIKE 查询完成,但可以用正则表达式完成

请参阅:在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;
}

最新更新