首先,我在WordPress中使用木材(细枝)。
我用Ajax创建了一个搜索查询。一切正常,但结果不适合我。有时结果会显示部分单词
示例:如果我写下;jour"E;我得到了;澳大利亚jour“d\'hui”;相似的结果。
我想作为结果只有整句话。
在这里做了研究之后。指示使用\'exact\' => 1
我在我的查询中尝试了它,但现在我在每次搜索时不再有任何结果(0个结果),而以前我有很好的结果。
你知道怎么了吗?非常感谢。
脚本。js公司
$(\'#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(\'\');
}
});
功能。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),
\'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
));
}
Timber::render( \'bloc_fetch.twig\', $context );
die();
}
搜索。细枝
<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>