WordPress query search 's' exact word

sz81bmfz  于 2023-08-03  发布在  WordPress
关注(0)|答案(3)|浏览(119)

我想限制关键字搜索,只搜索确切的单词,而不是短语或混合内容,所以如果我搜索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']是搜索关键字的标准查询,但我尝试过的一些标准查询修改如下所示:

还有其他的例子,但没有一个对我有用。
我如何修改它,使它只能搜索内容中的确切单词?
我看到这个问题已经发布了很多次,但我尝试过的所有例子都不起作用。

e4eetjau

e4eetjau1#

熟悉SQL语法,更容易使用WordPress执行任何查询
您可以使用posts_clauses过滤器直接修改任何查询,包括搜索查询
默认情况下,WordPress执行搜索的文章标题,文章内容和文章节选使用LIKE%环绕搜索词,类似的语法如下

WHERE 1=1 
AND (
    post_table.post_title LIKE '%hello%' OR 
    post_table.post_excerpt LIKE '%hello%' OR
    post_table.content LIKE '%hello%'
)

字符串
它甚至将每个单词分解为一个单独LIKE语句,
但是因为你想做一个精确的匹配,你可以在搜索词之间添加一个空格,然后如果是第一个或最后一个,则另一个匹配,就像下面在post_content列中搜索一个字符串

WHERE 1=1 
AND (
    post_table.post_content LIKE 'hello %' OR  //hello is first word, match on -> hello world
    post_table.post_content LIKE '% hello %' OR //hello is in between words, match on -> world hello world
    post_table.post_content LIKE '% hello' //hello is the last word, match on -> world hello
)


或者,如果你使用的是支持正则表达式的MYSQL 8,你可以执行带边界的字符串搜索,比如;

WHERE 1=1 
AND (
    post_table.post_content RLIKE "[[:<:]]hello[[:>:]]"
)


现在修改seach查询真的很简单,代码参考如下

add_filter( 'posts_clauses', function ($clauses, $query) {
    
    // prevent query modification if its not search page or an admin page or not main query
    if ( !$query->is_search || is_admin() || !$query->is_main_query()) 
            return $clauses;
    
    // get the search term
    $searchTerm = sanitize_text_field( $query->query['s'] );
    
    //build the search query, and perfom the search on post_title and post_content column
    //refer to two functions below
    $searchQuery = _custom_search_query($searchTerm, ['post_title', 'post_content']);
    
    //override wordpress default search query by defining the where key on its clauses
    $clauses['where'] = "AND ( $searchQuery )";
    
    // additional filter on what you want to search
    $clauses['where'] .= " AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'";
    
    //add the default order
    $clauses['orderby'] = "wp_posts.post_title DESC, wp_posts.post_date DESC";
    
    return $clauses;
}, 10, 2 );


为了避免手动输入所有的LIKESOR's,我们可以根据您想要的查询方式使用下面的这两个函数,
用于在指定搜索的不同字段上有条件地构建LIKEOR语句

function _custom_search_query( $term, $fields = [] ) {
    
    $query = '';
    $compares = ["$term %", "% $term", "% $term %"];
    foreach ($fields as $key => $field) {
        foreach ($compares as $k => $comp) {
            $query .= ($key > 0 || $k > 0 ? ' OR' : '' ). " wp_posts.$field LIKE '$comp'";
        }
    }
    return $query;
}


用于在指定搜索的不同字段上有条件地构建RLIKEOR语句

function _regex_custom_search_query(  $term, $fields = [] ) {
    $query = '';
    foreach ($fields as $key => $field) {
        $query .= ($key > 0 ? ' OR' : '' ). ' wp_posts.'.$field.' RLIKE "[[:<:]]'.$term.'[[:>:]]"' ;
    }
    return $query;
}


另外,在前端搜索页面上打印$wp_query->request,您将看到SQL在当前页面上执行,这将帮助您调试任何问题。

add_action('wp_head', function() {
    global $wp_query;

    if ( !$wp_query->is_search )
        return;
    
    echo '<pre>', print_r($wp_query->request, 1), '</pre>';
    
});


如果您还想创建自己的搜索页面,则只需使用$wpdb应用相同的查询即可。

moiiocjp

moiiocjp2#

要限制WordPress中的搜索精确匹配词而不是短语或混合内容,您可以在主题的functions.php文件或自定义插件中使用以下代码片段:

function exact_word_search_filter($query) {
    if ($query->is_search) {
        $search_terms = explode(' ', $query->query_vars['s']);
        $search_terms = array_map('esc_sql', $search_terms);
        $exact_search = implode(' ', $search_terms);
        $query->set('exact', true);
        $query->set('search_terms', "{$exact_search}");
        $query->set('search_terms_count', 1);
    }
    return $query;
}
add_filter('pre_get_posts', 'exact_word_search_filter');

字符串
此代码修改搜索查询,将exact参数设置为true,以确保搜索结果中只包含精确匹配的单词。

dwbf0jvd

dwbf0jvd3#

要实现这一点,需要将posts_search钩子修改为正则表达式。所以这将匹配整个单词。您需要为此执行一个自定义SQL查询。

function keyword_search( $search, $wp_query ) {
    global $wpdb;

    if ( empty( $search ) )
        return $search;

    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search = $searchand = '';

    foreach ( (array) $q['search_terms'] as $term ) {
        $term = $n . $wpdb->esc_like( $term ) . $n;
        $search .= $wpdb->prepare( "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]'", $term );
        $searchand = ' AND ';
    }
    return $search;
}

字符串
然后替换get_job_listing函数条件

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;
    add_filter( 'posts_search', 'keyword_search', 500, 2 );
}


并在搜索后删除搜索过滤器。使用这个

do_action( 'after_get_job_listings', $query_args, $args );
remove_filter( 'posts_search', 'keyword_search', 500 );


测试并为我工作。我希望这也能为你工作。

相关问题