First of all, I use Timber (TWIG) with WordPress.
I created a search query in Ajax. Everything works fine but the results do not suit me. Sometimes the results show me part of the words
Example : if I write "jour" I got "aujourd'hui" like result.
I would like to have as result only the whole words.
After doing my research here. It's indicated to use 'exact' => 1
I tried it in my query but now I no longer have any results (0 result) at each search whereas before I had the good results.
Do you have any idea about what's wrong ? Thank you.
script.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('');
}
});
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),
'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();
}
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>