помогите пожалуйста с такой проблемой.
На странице есть фильтр для поиска домов, по микрорайону(это рубрики), цене и площади. После отправки запроса по AJAX, вывожу дома, но сейчас это не работает, выбираю нужную категорию, а в результате всё равно получаю все зарегистрированные дома, а не конкретно выбранного микрорайона, где у меня ошибка?
Страница с архивом домов
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<?php
if( $terms = get_terms( array( 'taxonomy' => 'category', 'orderby' => 'name' ) ) ) :
echo '<select name="categoryfilter"><option value="">Select category...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
?>
<input type="text" name="price_min" placeholder="Min price" />
<input type="text" name="price_max" placeholder="Max price" />
<button>Apply filter</button>
<input type="hidden" name="action" value="myfilter">
JS файл
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('button').text('Processing...'); // changing the button label
},
success:function(data){
filter.find('button').text('Apply filter'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
functions.php
function new_filter_function(){
$args = array(
'orderby' => 'date'
);
// categories
if( isset( $_POST['categoryfilter'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
if( isset( $_POST['price_min'] ) && $_POST['price_min'] || isset( $_POST['price_max'] ) && $_POST['price_max'] )
$args['meta_query'] = array( 'relation'=>'AND' );
if( isset( $_POST['price_min'] ) && $_POST['price_min'] && isset( $_POST['price_max'] ) && $_POST['price_max'] ) {
$args['meta_query'][] = array(
'key' => '_price',
'value' => array( $_POST['price_min'], $_POST['price_max'] ),
'type' => 'numeric',
'compare' => 'between'
);
} else {
if( isset( $_POST['price_min'] ) && $_POST['price_min'] )
$args['meta_query'][] = array(
'key' => '_price',
'value' => $_POST['price_min'],
'type' => 'numeric',
'compare' => '>'
);
if( isset( $_POST['price_max'] ) && $_POST['price_max'] )
$args['meta_query'][] = array(
'key' => '_price',
'value' => $_POST['price_max'],
'type' => 'numeric',
'compare' => '<'
);
}
$query = new WP_Query( [
'post_type' => 'houses',
] );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<div class="slider__item">
<div class="slider__img">
<a class="slider__link" href="#"></a>
<img src="img/slider1.png" alt="дом">
</div>
<div class="slider__info">
<div class="info-name">
<span class="info-micro">новые сады</span>
<div class="info-discription">
<?php the_title(); ?>
</div>
<div class="info-icons">
<div class="info-icons-item beds">
<img src="img/bed.png" alt="спальни">
<span>2</span>
</div>
<div class="info-icons-item bathrooms">
<img src="img/shower.png" alt="ванные">
<span>2</span>
</div>
</div>
</div>
<div class="info-price">
<div class="area">
45<span class="postfix">м<sup>2</sup></span>
</div>
<div class="main-price">
3 500 000<span class="postfix">₽</span>
</div>
<div class="metr-price">
45 000<span class="postfix">₽ / м<sup>2</sup></span>
</div>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}