创建一个新帖子并发布它。
标题是我的搜索测试
,内容如下:
no host route
检查 wordpress 数据库中发生的情况。
select post_title from wp_posts
where post_content like "%no%"
and post_content like "%route%"
and post_content like "%to%"
and post_content like "%host%";
名为 my test for search
的帖子不会出现在选择结果中。
在wordpress搜索栏输入no route to host
,点击回车。
结果显示为名为 my test for search
的帖子。
我找到了网页包含to
的原因,在左上角,有一个单词Customize
,里面包含了搜索到的单词to
.
如何在 wordpress 搜索栏中更改这样的搜索 Action ?
我想在wordpress saerch bar中进行搜索行为,例如当你输入no route to host
时,等于下面的sql命令。
select post_title from wp_posts where post_content like "%no%route%to%host%";
我wordpress中的所有插件。
CodePen Embedded Pens Shortcode
Crayon Syntax Highlighter
Disable Google Fonts
Quotmarks Replacer
SyntaxHighlighter Evolved
最佳答案
在 wp-includes/class-wp-query.php:1306
上的 SQL WHERE 子句中添加了这个 :
<?php
// wp-includes/class-wp-query.php:~1306
foreach ( $q['search_terms'] as $term ) {
//...
$like = $n . $wpdb->esc_like( $term ) . $n;
$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 );
// ...
因此,我会连接到 pre_get_posts
,并将查询的词作为明确的“search_terms
”提供,因为它们被添加到该子句中,使用 LIKE
修饰符,正如您所说的那样!
所以,我们可能会这样做:
<?php
// functions.php
function fuzzify_query(\WP_Query $q) {
if (true === $q->is_search()
&& true === property_exists($q, 'query')
&& true === key_exists('s', $q->query)
) {
$original_query = $q->query['s'];
$words = explode(' ', $original_query);
$fuzzy_words = array_map(
function($word) {
return '%'.$word.'%';
},
$words
);
$q->query_vars['search_terms'] = $fuzzy_words;
return $q;
}
return $q;
}
add_action('pre_get_posts', 'fuzzify_query', 100); // Or whatever priority your fuzziness requires!
关于mysql - 如何更改 wordpress 搜索栏中的搜索操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53881132/