Answer the question
In order to leave comments, you need to log in
Search only within a given Wordpress taxonomy?
The site has two custom taxonomies: movies and books. Now I use the standard wordpress search form with the Ajax search plugin (like a live search):
<form method="get" class="searchform" action="<?php bloginfo('url'); ?>" >
<input type="text" class="place s" name="s" id='my-s' autocomplete="off" placeholder="Поиск" />
<input type="image" src="search.png" class="submit-s" name="submit" value="<?php _e('Найти'); ?>" />
</form>
Answer the question
In order to leave comments, you need to log in
The form proposed by Roman Crabbs only passes the required parameter through the hidden field. You also need to modify the query itself:
function search_filter( $query ) {
// Берем название таксономии из скрытого поля
$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
// Получаем ID всех терминов в заданной таксономи
$terms = get_terms( $taxonomy, array(
'fields' => 'ids'
) );
// Изолируем нужный запрос
if ( !is_admin() && $query->is_main_query() && $query->is_search ) {
// Формируем массив параметров подзапроса по таксономии
$tax_query = array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $terms,
'operator' => 'IN'
),
);
// Передаем параметры подзапроса в основной запрос
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'search_filter' );
<form method="get" class="searchform" action="<?php bloginfo('url'); ?>" >
<input type="text" class="place s" name="s" id='my-s' autocomplete="off" placeholder="Поиск" />
<input type="image" src="search.png" class="submit-s" name="submit" value="<?php _e('Найти'); ?>" />
<input type="hidden" name="taxonomy" value="films" />
</form>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question