我想限制关键字搜索,只搜索确切的单词,而不是短语或混合内容,所以如果我搜索cheese
,它需要限制只搜索单词cheese
,现在它也会返回单词cheeseburger
的内容。
我修改后的模板查询中的搜索查询部分就在这里:
if ( ! function_exists( 'get_job_listings' ) ) :
if ( 'top_rating' === $args['orderby'] ) {
$query_args['meta_query'] = [
'relation' => 'OR',
[
'key' => 'rating',
'compare' => 'EXISTS',
],
[
'key' => 'rating',
'compare' => 'NOT EXISTS',
],
];
$query_args['orderby'] = [
'meta_value_num' => 'DESC',
'comment_count' => 'DESC',
];
}
$job_manager_keyword = sanitize_text_field( $args['search_keywords'] );
if ( ! empty( $job_manager_keyword ) && strlen( $job_manager_keyword ) >= apply_filters( 'job_manager_get_listings_keyword_length_threshold', 2 ) ) {
$search_query = $job_manager_keyword;
$query_args['s'] = $search_query;
// $query_args['s'] = '"' . $search_query . '"';
// $query_args['s'] = '/\b' . preg_quote($search_query, '/') . '\b/';
$query_args['sentence'] = true;
add_filter( 'posts_search', 'get_job_listings_keyword_search' );
}
$query_args = apply_filters( 'job_manager_get_listings', $query_args, $args );
if ( empty( $query_args['meta_query'] ) ) {
unset( $query_args['meta_query'] );
}
if ( empty( $query_args['tax_query'] ) ) {
unset( $query_args['tax_query'] );
}
/** This filter is documented in wp-job-manager.php */
$query_args['lang'] = apply_filters( 'wpjm_lang', null );
// Filter args.
$query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args );
do_action( 'before_get_job_listings', $query_args, $args );
// Cache results.
if ( apply_filters( 'get_job_listings_cache_results', true ) ) {
$to_hash = wp_json_encode( $query_args );
$query_args_hash = 'jm_' . md5( $to_hash . JOB_MANAGER_VERSION ) . WP_Job_Manager_Cache_Helper::get_transient_version( 'get_job_listings' );
$result = false;
$cached_query_results = true;
$cached_query_posts = get_transient( $query_args_hash );
if ( is_string( $cached_query_posts ) ) {
$cached_query_posts = json_decode( $cached_query_posts, false );
if (
$cached_query_posts
&& is_object( $cached_query_posts )
&& isset( $cached_query_posts->max_num_pages )
&& isset( $cached_query_posts->found_posts )
&& isset( $cached_query_posts->posts )
&& is_array( $cached_query_posts->posts )
) {
if ( in_array( $query_args['fields'], [ 'ids', 'id=>parent' ], true ) ) {
// For these special requests, just return the array of results as set.
$posts = $cached_query_posts->posts;
} else {
$posts = array_map( 'get_post', $cached_query_posts->posts );
}
$result = new WP_Query();
$result->parse_query( $query_args );
$result->posts = $posts;
$result->found_posts = intval( $cached_query_posts->found_posts );
$result->max_num_pages = intval( $cached_query_posts->max_num_pages );
$result->post_count = count( $posts );
}
}
if ( false === $result ) {
$result = new WP_Query( $query_args );
$cached_query_results = false;
$cacheable_result = [];
$cacheable_result['posts'] = array_values( $result->posts );
$cacheable_result['found_posts'] = $result->found_posts;
$cacheable_result['max_num_pages'] = $result->max_num_pages;
set_transient( $query_args_hash, wp_json_encode( $cacheable_result ), DAY_IN_SECONDS );
}
if ( $cached_query_results ) {
// random order is cached so shuffle them.
if ( 'rand_featured' === $args['orderby'] ) {
usort( $result->posts, '_wpjm_shuffle_featured_post_results_helper' );
} elseif ( 'rand' === $args['orderby'] ) {
shuffle( $result->posts );
}
}
} else {
$result = new WP_Query( $query_args );
}
do_action( 'after_get_job_listings', $query_args, $args );
//remove_filter( 'posts_search', 'get_job_listings_keyword_search' );
return $result;
}
endif;
字符串
我可以看到$query_args['s']
是搜索关键字的标准查询,但我尝试过的一些标准查询修改如下所示:
- Make a WordPress query search match exactly the search term(第一个字符)
- WordPress exact search query
- Query for exact word search
还有其他的例子,但没有一个对我有用。
我如何修改它,使它只能搜索内容中的确切单词?
我看到这个问题已经发布了很多次,但我尝试过的所有例子都不起作用。
3条答案
按热度按时间e4eetjau1#
熟悉SQL语法,更容易使用WordPress执行任何查询
您可以使用posts_clauses过滤器直接修改任何查询,包括搜索查询
默认情况下,WordPress执行搜索的文章标题,文章内容和文章节选使用
LIKE
和%
环绕搜索词,类似的语法如下字符串
它甚至将每个单词分解为一个单独LIKE语句,
但是因为你想做一个精确的匹配,你可以在搜索词之间添加一个空格,然后如果是第一个或最后一个,则另一个匹配,就像下面在
post_content
列中搜索一个字符串型
或者,如果你使用的是支持正则表达式的MYSQL 8,你可以执行带边界的字符串搜索,比如;
型
现在修改seach查询真的很简单,代码参考如下
型
为了避免手动输入所有的
LIKES
和OR's
,我们可以根据您想要的查询方式使用下面的这两个函数,用于在指定搜索的不同字段上有条件地构建
LIKE
和OR
语句型
用于在指定搜索的不同字段上有条件地构建
RLIKE
和OR
语句型
另外,在前端搜索页面上打印
$wp_query->request
,您将看到SQL在当前页面上执行,这将帮助您调试任何问题。型
如果您还想创建自己的搜索页面,则只需使用
$wpdb
应用相同的查询即可。moiiocjp2#
要限制WordPress中的搜索精确匹配词而不是短语或混合内容,您可以在主题的
functions.php
文件或自定义插件中使用以下代码片段:字符串
此代码修改搜索查询,将
exact
参数设置为true
,以确保搜索结果中只包含精确匹配的单词。dwbf0jvd3#
要实现这一点,需要将posts_search钩子修改为正则表达式。所以这将匹配整个单词。您需要为此执行一个自定义SQL查询。
字符串
然后替换get_job_listing函数条件
型
并在搜索后删除搜索过滤器。使用这个
型
测试并为我工作。我希望这也能为你工作。