简体   繁体   English

更改搜索查询wordpress

[英]change search query wordpress

I have a search query that is running on through a custom post type, but its search for all parts of text, not full sentences. 我有一个通过自定义帖子类型运行的搜索查询,但是它搜索文本的所有部分,而不是完整的句子。

Eg if you search for Menta as a company name, it will also display an entry with Comple menta ry in the text content. 例如,如果您搜索Menta作为公司名称,它也会在文本内容中显示一个带有Comple menta的条目。

I need to find a way to amend this function below to either only search the post title and nothing else, which would be the ideal solution. 我需要在下面找到一种方法来修改此功能,使其仅搜索帖子标题,而不进行其他任何搜索,这将是理想的解决方案。 Or if thats not possible, then to only search for full words not parts. 或者,如果那不可能,则仅搜索完整单词而不是部分内容。 I hope that makes sense. 我希望这是有道理的。

This is the code: 这是代码:

    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;
    }

Any help would be great :D 任何帮助都很好:D

This function looks like it aims to add in meta searching, not to restrict the existing search to the post title only. 该功能看起来像是要添加元搜索,而不是将现有搜索仅限于帖子标题。 If you want to do that, try removing the code you have and using the code here: 如果要这样做,请尝试删除已有的代码,然后在此处使用代码:

Make wordpress search only in post title 仅在帖子标题中进行wordpress搜索

The full word vs partial match can't be done with a LIKE query, but can be done with regex 全字vs部分匹配不能通过LIKE查询来完成,但是可以通过正则表达式来完成

See: Search for "whole word match" in MySQL 请参阅: 在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM