Answer the question
In order to leave comments, you need to log in
How to save filtering?
Made filtering by custom taxonomy:
<?php
function posts_filters(){
$args = array(
'orderby' => 'date',
'order' => $_POST['date']
);
if( isset( $_POST['categoryfilter'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'filter',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
$query = new WP_Query( $args );
if( $query->have_posts('projects') ) :
echo '<ul>';
while( $query->have_posts() ): $query->the_post();
echo '<li><a href="' . get_permalink( $query->post->ID ) . '">' . $query->post->post_title . '</a></li>';
endwhile;
echo '</ul>';
wp_reset_postdata();
else :
echo 'Записей не найдено';
endif;
die();
}
add_action('wp_ajax_customfilter', 'posts_filters');
add_action('wp_ajax_nopriv_customfilter', 'posts_filters');
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="post-date-filter">
<?php
if( $terms = get_terms( 'filter', 'orderby=name' ) ) :
echo '<select name="categoryfilter"><option>Выберите категорию...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
?>
<label>
<input type="radio" name="date" value="ASC" /> Дата: по возрастанию
</label>
<label>
<input type="radio" name="date" value="DESC" selected="selected" /> Дата: по убыванию
</label>
<button>Применить фильтр</button>
<input type="hidden" name="action" value="customfilter">
</form>
<div id="filtering-results"></div>
</div>
</section>
<script>
jQuery(function($){
$('#post-date-filter').submit(function(){
var filter = $('#post-date-filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // данные формы
type:filter.attr('method'), // POST
beforeSend:function(xhr){ filter.find('button').text('Применяем фильтр...'); },
success:function(data){ filter.find('button').text('Применить фильтр'); $('#filtering-results').html(data); }
});
return false;
});
});
</script>
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