Answer the question
In order to leave comments, you need to log in
How to make page link changes when filtering posts?
Good time. A problem arose.
The plugin "Advansed custom fields" is installed on the site, through it the "Real Estate" section and all the resulting fields (number of rooms, cost, area, area, etc.) are
implemented. Records filter is implemented on the catalog pages. It works as it should, automatically pulls up all objects. There are no questions here. There is a question about links. The link to the page does not change, but I would like the link to change when sorting is applied and it can be sent there to the client or manager.
There is a plugin "Search & Filter Pro" very similar, but due to the "ingenuity" of those who made the site, the plugin will not work as it should. Already tested.
The filter code itself looks like this.
functions.php
function true_filter_function(){
//echo $_POST['country'];
$args = array(
'post_type' => 'hotel',
'meta_key' => 'price',
'orderby' => 'meta_value', // сортировка по дате у нас будет в любом случае (но вы можете изменить/доработать это)
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'slug',
'terms' => $_POST['country']
)
),
$args['meta_query'] = array( 'relation'=>'AND' )
);
$more_than_one = false;
// // создаём массив $args['meta_query'] если указана хотя бы одна цена или отмечен чекбокс
// if( isset( $_POST['cena_min'] ) || isset( $_POST['cena_max'] ) || ( isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' ) )
// $args['meta_query'] = array( 'relation'=>'AND' ); // AND значит все условия meta_query должны выполняться
//
// условие 1: цена больше $_POST['cena_min']
if($_POST['bed_quantity'] != 'none') {
$compare = '=';
if ($_POST['bed_quantity'] == '5') {
$compare = '>=';
}
$args['meta_query'][] = array(
'key' => 'kol-vo_spalen',
'compare' => $compare,
'value' => $_POST['bed_quantity'],
'type' => 'numeric',
);
}
if($_POST['object_type'] != 'none') {
$args['meta_query'][] = array(
'key' => 'object-type-filter',
'compare' => '=',
'value' => $_POST['object_type'],
'type' => 'CHAR',
);
}
if($_POST['region'] != 'none') {
$args['meta_query'][] = array(
'key' => 'hotel-region',
'compare' => '=',
'value' => $_POST['region'],
'type' => 'CHAR',
);
}
if($_POST['sea_distance'] != 'none') {
$compare = '<=';
if ($_POST['sea_distance'] == '6000') {
$compare = '>=';
}
$args['meta_query'][] = array(
'key' => 'sea-distance',
'compare' => $compare,
'value' => (int)$_POST['sea_distance'],
'type' => 'numeric',
);
}
if($_POST['bassein'] != 'none') {
$args['meta_query'][] = array(
'key' => 'bassein',
'compare' => '=',
'value' => (int)$_POST['bassein'],
'type' => 'numeric',
);
}
if($_POST['price'] != 'none') {
$price = explode('-',$_POST['price']);
$min_price = $price[0];
$max_price = $price[1];
if ($max_price === '+') {
$args['meta_query'][] = array(
'key' => 'price',
'compare' => '>=',
'value' => (int)$min_price,
'type' => 'numeric',
);
}
else {
$args['meta_query'][] = array(
'key' => 'price',
'compare' => '>=',
'value' => (int)$min_price,
'type' => 'numeric',
);
$args['meta_query'][] = array(
'key' => 'price',
'compare' => '<=',
'value' => (int)$max_price,
'type' => 'numeric',
);
}
}
$query = new WP_Query($args);
$action_from_count = get_field('action-form-number', 'option');
$count = 1;
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-lg-4" style="position: relative">
<div class="single-destinations-list style-four">
<a href="<?php the_permalink(); ?>" class="wrapper-link"> </a>
<div class="blur-thumb" style="background-image:url(<?php echo get_the_post_thumbnail_url(); ?>);"></div>
<div class="details dark">
<p class="location"><img src="<?php echo get_template_directory_uri(); ?>/assets\img\icons\1.png" alt="map"><?php the_field('hotel-region'); ?></p>
<h4 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<p class="content"><?php echo get_the_excerpt(); ?></p>
<div class="list-price-meta">
<ul class="tp-list-meta d-inline-block">
<!-- <li><i class="fa fa-calendar-o"></i> 8oct</li>-->
<li><i class="fa fa-university"></i> <?php the_field('square'); ?>m2</li>
<li><i class="fa fa-bed"></i> <?php the_field('kol-vo_spalen'); ?></li>
<!-- <?php if ( get_field('bassein')) { ?>
<li><i class="fa fa-bath"></i></li>
<?php } ?>-->
</ul>
<div class="tp-price-meta d-inline-block">
<p>Цена</p>
<h2><?php the_field('price'); ?><span> €</span></h2>
</div>
</div>
</div>
</div>
</div>
<?php
//echo $count%$action_from_count;
if ($count%$action_from_count === 0) {
$count = 0; ?>
<div class="col-12 d-block d-md-none">
<!-- newslatter area Start -->
<div class="newslatter-area pd-top-30">
<div class="container">
<div class="newslatter-area-wrap border-tp-solid">
<div class="row">
<div class=" col-md-5 offset-md-1 offset-0">
<div class="section-title mb-md-0">
<h3 class="title">Получить каталог</h3>
<p>Подберем для Вас наилучшие варианты</p>
</div>
</div>
<div class="col-md-5 align-self-center action-form">
<?php
$catalog_form = get_field('country-shortcode', 'option');
echo do_shortcode("$catalog_form");
?>
</div>
</div>
</div>
</div>
</div>
<!-- newslatter area End -->
</div>
<?php }
$count++;
endwhile;
endif;
if ( $query->max_num_pages > 1 ) : ?>
<script>
var ajaxurl = '<?php echo site_url() ?>/wp-admin/admin-ajax.php';
var true_posts = '<?php echo serialize($query->query_vars); ?>';
var current_page = <?php echo (get_query_var('paged')) ? get_query_var('paged') : 1; ?>;
var max_pages = '<?php echo $query->max_num_pages; ?>';
jQuery(function($){
$('#true_loadmore').click(function(){
$(this).text('Загружаю...'); // изменяем текст кнопки, вы также можете добавить прелоадер
var data = {
'action': 'loadmore',
'query': true_posts,
'page' : current_page
};
$.ajax({
url:ajaxurl, // обработчик
data:data, // данные
type:'POST', // тип запроса
success:function(data){
if( data ) {
$('#true_loadmore').text('Загрузить ещё'); // вставляем новые посты
$('#true_loadmore_wrapper').before(data); // вставляем новые посты
current_page++; // увеличиваем номер страницы на единицу
if (current_page == max_pages) $("#true_loadmore").remove(); // если последняя страница, удаляем кнопку
} else {
$('#true_loadmore').remove(); // если мы дошли до последней страницы постов, скроем кнопку
}
}
});
});
});
</script>
<div class="col-12 text-center" id="true_loadmore_wrapper">
<div id="true_loadmore">Загрузить ещё</div>
</div>
<?php endif;
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question