您可以尝试使用posts_search
按照@Kaiser在您提供的第一个链接中的优雅建议进行筛选。这里有一个想法:
function filter_search($sql){
global $wpdb;
if( strpos(get_query_var("s"),"lass") !== false AND strpos(get_query_var("s"),"class") === false ){
$sql .= " AND {$wpdb->posts}.post_title NOT LIKE \'%class%\'";
$sql .= " AND {$wpdb->posts}.post_content NOT LIKE \'%class%\'";
}
return $sql;
}
add_filter( \'posts_search\', \'filter_search\');
检查搜索词是否包含
lass
而不是
class
然后修改搜索查询以排除
class
.
SQL中的更改如下所示:
Before:
AND (((wp_posts.post_title LIKE \'%lass%\') OR (wp_posts.post_content LIKE \'%lass%\')))
After:
AND (((wp_posts.post_title LIKE \'%lass%\') OR (wp_posts.post_content LIKE \'%lass%\')))
AND wp_posts.post_title NOT LIKE \'%class%\' AND wp_posts.post_content NOT LIKE \'%class%\'
更新:您可以尝试
function filter_search($sql){
global $wpdb;
if( strpos(get_query_var("s"),"lass") !== false AND strpos(get_query_var("s"),"class") === false ){
$sql .= " AND ( {$wpdb->posts}.post_content LIKE \'lass %\'";
$sql .= " OR {$wpdb->posts}.post_content LIKE \'% lass %\'";
$sql .= " OR {$wpdb->posts}.post_content LIKE \'% lass\' )";
}
return $sql;
}
add_filter( \'posts_search\', \'filter_search\');
如果你想准确定位单词
lass
在帖子内容中。