微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress搜索功能-全词问题

如何解决WordPress搜索功能-全词问题

首先,我在wordpress中使用Timber(TWIG)。

我在Ajax中创建了一个搜索查询。一切正常,但结果不适合我。有时结果显示出部分单词。

示例:如果我写“ jour ”,则会得到类似“ au jour d'hui”的结果。

我只希望整个单词都包含在内。

在这里进行研究之后。已指示使用'exact' => 1

我在查询中尝试过此方法,但现在不再有任何结果(0个结果) 每次搜索之前,我就没有好结果。

您对什么地方有任何想法吗?谢谢。

script.js

function init_datas_fetch() {
$('#search-form').on('submit',function(e) {
    e.preventDefault();

    var searchValue = $('#search-input').val();

    if (searchValue.length > 0) {
        $.ajax({
            type: 'POST',url: '/wp-admin/admin-ajax.PHP',dataType: 'html',data: {
                'action' : 'datas_fetch','terms' : searchValue
            },success: function(data) {
                $('#fetch-list').html(data);
            },error: function(data) {

            }
        });
    } else {
        $('#fetch-list').html('');
    }
});
}

functions.PHP

add_action( 'wp_ajax_nopriv_datas_fetch','datas_fetch' );
add_action( 'wp_ajax_datas_fetch','datas_fetch' );

function datas_fetch() {
    $context['page_search'] = true;
    $context['terms'] = isset($_POST['terms']) ? $_POST['terms'] : '';

    $posts_types_selected = array('pages' => 'page','articles' => 'post','videos' => 'videos','podcasts' => 'podcasts');

    $exclude_posts = get_field('search_exclude','option');

    if ($_POST['terms']) {
        $context['posts'] = Timber::get_posts(array(
            'post_type' => array_values($posts_types_selected),'post_status' => 'publish','posts_per_page' => -1,'paged' => 1,'orderby' => 'date','order' => 'DESC','post__not_in' => array_values($exclude_posts),'hide_empty' => true,'has_password' => FALSE,'s' => $context['terms'],'exact' => 1
        ));
    } else {
        $context['posts'] = Timber::get_posts(array(
            'post_type' => array_values($posts_types_selected),'has_password' => FALSE
        ));
    }

    Timber::render( 'bloc_fetch.twig',$context );

    die();
}

tpl_search.twig

<form role="search" id="search-form">
    <input type="text" id="search-input" class="search__input" placeholder="{{ __('Rechercher des articles,vidéos...','cmd') }}" value="">
    <input type="submit" id="search-submit" class="search__button" value="Rechercher">
</form>

<div id="fetch-list">
 {% include "bloc_fetch.twig" %}
</div>

bloc_fetch.twig

{% for card in posts %}
    <div class="card">
        <img src="{{ card.thumbnail.src }}" class="card__visual" alt="{{ card.thumbnail.alt }}">
        <a href="{{ card.link }}" class="card__link"></a>
        <div class="card__content">
            {{ card.article_exceprt }}
        </div>
    </div>
{% endfor %}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。