Благодаря переполнению стека я смог заставить свой фильтр работать (Ajax-фильтр для wordpress), но теперь я хочу добавить поисковый ввод, который ищет заголовки моих сообщений (пользовательский тип сообщения под названием «Contratistas»). . Итак, у меня есть несколько вопросов:
<form id = "searchform" method = "get" action = "<?php echo esc_url( home_url( '/' ) ); ?>">
<input type = "text" class = "search-field mb-3" name = "s" placeholder = "Search" value = "<?php echo get_search_query(); ?>">
<input type = "submit" value = "Search" class = "mb-3">
</form>
<form action = "<?php echo site_url() ?>/wp-admin/admin-ajax.php" method = "POST" id = "filter">
<div class = "titulo mb-3">
<h3>Región</h3>
</div>
<?php
if ( $terms = get_terms( array( 'taxonomy' => 'region', 'orderby' => 'name' ) ) ) :
echo '<select name = "filtroRegion"><option value = "">Seleccione una región</option>';
foreach ( $terms as $term ) :
echo '<option value = "' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<div class = "titulo my-3">
<h3>Industrias</h3>
</div>
<?php
if ( $terms = get_terms( array( 'taxonomy' => 'industria', 'orderby' => 'name' ) ) ) :
echo '<select name = "filtroIndustria"><option value = "">Seleccione una industria</option>';
foreach ( $terms as $term ) :
echo '<option value = "' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<button class = "my-3 filtrar">Filtrar</button>
<button><a href = "" id = "clear">Limpiar filtros</a></button>
<input type = "hidden" name = "action" value = "myfilter">
</form>
Как видите, у меня есть две кнопки, но мне нужна только последняя ("Фильтрар").
Вот мой фильтр:
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
if (!empty($_POST['filtroRegion']) || !empty($_POST['filtroIndustria'])) {
$args['tax_query'] = array();
if (!empty($_POST['filtroRegion'])) {
$args['tax_query'][] = array(
'taxonomy' => 'region',
'terms' => $_POST['filtroRegion']
);
}
if (!empty($_POST['filtroIndustria'])) {
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'terms' => $_POST['filtroIndustria']
);
}
if (count($args['tax_query']) > 1) {
$args['tax_query']['relation'] = 'AND';
}
}
$query = new WP_Query($args);
И мой JQuery:
jQuery(function($) {
$('#filter').submit(function() {
var filter = $('#filter');
$.ajax({
url: filter.attr('action'),
data: $('#filter :input').filter(function(index, element) { return $(element).val() != ''; }).serialize(), // form data
type: filter.attr('method'), // POST
beforeSend: function(xhr) {
filter.find('.filtrar').text('Procesando...'); // changing the button label
},
success: function(data) {
filter.find('.filtrar').text('Filtrar'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
}
}
Любая помощь или руководство будут очень признательны. Спасибо
Я понял! Вот мой рабочий код на случай, если он кому-то еще понадобится! Ответ был ОЧЕНЬ прост! Мне оставалось только добавить это:
's' => $_POST['s']
Вот моя функция
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista',
's' => $_POST['s'],
);
//Same code as before:
if ( !empty($_POST['filtroRegion']) || !empty($_POST['filtroIndustria']) ){
$args['tax_query'] = array();
if (!empty($_POST['filtroRegion']) ){
$args['tax_query'][] = array(
'taxonomy' => 'region',
'terms' => $_POST['filtroRegion']
);
}
}
$query = new WP_Query( $args );
//my post
И моя форма:
<form action = "<?php echo site_url() ?>/wp-admin/admin-ajax.php" method = "POST" id = "filter">
<h3>Buscar</h3>
<input type = "text" class = "search-field mb-3" name = "s" placeholder = "Busqueda por nombre" value = "<?php echo get_search_query(); ?>">
<div class = "titulo mb-3">
<h3>Región</h3>
</div>
<?php
if ( $terms = get_terms( array( 'taxonomy' => 'region', 'orderby' => 'name' ) ) ) :
echo '<select name = "filtroRegion"><option value = "">Seleccione una región</option>';
foreach ( $terms as $term ) :
echo '<option value = "' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>