Answer the question
In order to leave comments, you need to log in
AJAX filter on custom types and custom fields in wordpress?
Hello. When creating an AJAX filter by custom fields, there was a problem, the request is sent but there is no response, I follow the instructions https://misha.blog/wordpress/filtryi-zapisey-i-tov...
Tell me what I'm doing wrong?
I suspect that the $args array is not being passed back, I don’t know how to pass it and add it correctly to query_posts on the page?
Thanks in advance.
Here is the code of the page with filters
form
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<div class="form-group">
<label for="dateStart">С даты</label>
<input type="date" class="form-control" id="dateStart" name="dateStart">
</div>
<div class="form-group">
<label for="dateEnd">По дату</label>
<input type="date" class="form-control" id="dateEnd" name='dateEnd'>
</div>
<div class="form-group">
<label for="seminarTypeSelect">Город</label>
<select id="seminarTypeSelect" class="form-control" name='city'>
<option>Москва</option>
<option>Киев</option>
</select>
</div>
<div class="form-group">
<label for="seminarTypeSelect2">Тип семнара</label>
<select id="seminarTypeSelect2" class="form-control" name='stype'>
<option value="seminar">Семинар</option>
<option value="webinar">Вебинар</option>
</select>
</div>
<button type="submit" class="btn btn-default">Применить фильтр</button>
<input type="hidden" name="action" value="myfilter">
</form>
<div id="response"></div>
</div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'kursy',
'order' => 'DESC',
'posts_per_page' => 15,
'orderby' => 'meta_value',
'paged' => $paged,
'meta_query' => array(
array('key' => 'datestart'),
),
);
?>
<script type="text/javascript">
jQuery(function ($) {
$('#filter').submit(function () {
var filter = $(this);
$.ajax({
url: ajaxurl, // обработчик
data: filter.serialize(), // данные
type: filter.attr('method'), // тип запроса
beforeSend: function (xhr) {
filter.find('button').text('Загружаю...'); // изменяем текст кнопки
},
success: function (data) {
filter.find('button').text(
'Применить фильтр'); // возвращаеи текст кнопки
$('#response').html(data);
}
});
return false;
});
});
</script>
function true_filter_function(){
$args['meta_query'] = array( 'relation'=>'AND' ); // AND значит все условия meta_query должны выполняться
// условие 1: Фильтр по датам если установленны.
if( isset( $_POST['dateStart'] ))
$args['meta_query'][] = array(
'key' => 'datestart',
'value' => $_POST['dateStart'],
'compare' => '=',
'type' => 'DATE'
);
if( isset( $_POST['stype'] ) )
$args['meta_query'][] = array(
'key' => 'stype',
'value' => $_POST['stype'],
'type' => 'CHAR',
'compare' => '='
);
die();
}
add_action('wp_ajax_myfilter', 'true_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'true_filter_function');
Answer the question
In order to leave comments, you need to log in
Misha has a lot of bugs in the Russian-language version, at least there are cuts in the code, he doesn’t even know about it himself, yes, yes!!...)))
If you don’t believe me, I can poke a link - the difference is a few lines, the script is fully functional destroyed.
Misha scored in the Russian Federation, scored ...
Google his English site - everything is OK there !!! Don't forget the dictionary.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question