Answer the question
In order to leave comments, you need to log in
How to filter through checkboxes in WordPress?
Hello, through trial and error, I managed to make some kind of filter for WordPress posts by tags. All the rules, but there was a trifle. You need to select tags through the checkbox, and not through the selector, as it is now. The whole problem is that due to the fact that I do not know PHP very well, I cannot do everything correctly. Namely, when I do it through the checkbox, I get all the entries that have tags. Tried through the slug parameter, but what it is, what it doesn't - it doesn't matter. Total: you need to make it possible to select several parameters in the filter. This is a normal filter, but it doesn't work for me. Most likely the solution is very close)
function.php
function posts_filters(){
$args = array(
'orderby' => 'date',
'order' => $_POST['date']
);
if( isset( $_POST['categoryfilter'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'post_tag',
'slag' => 'okna',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
if( isset( $_POST['check'] ) && $_POST['check'] == 'on' )
$args['tax_query'] = array(
array(
'taxonomy' => 'post_tag',
'slag' => 'okna',
'field' => 'id',
'terms' => $_POST['check']
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
echo '<div class="row">
<div class="col">
<div class="allLogoBrands">';
while( $query->have_posts() ): $query->the_post();
echo '<div><a href="' . get_permalink( $query->post->ID ) . '">' . the_post_thumbnail() . '</a></li>';
endwhile;
echo '</div>
</div>
</div>
</div>';
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( 'post_tag', '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="checkbox" name="check" /> Okna</label>
<button>Применить фильтр</button>
<input type="hidden" name="action" value="customfilter">
</form>
<div id="filtering-results"></div>
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;
});
});
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