Post filter with multiple checkbox taxonomy terms

I’m trying to apply the filter based on the multiple checkbox selected. But the filter is not working the way it should be. Please read the sanario below

— custom post type: cars

— custom taxonomy: categories

— term under car categories: best cars (contains 9 post. 3 for each car make)

— custom taxonomy: filter

— terms under filter: year(2016, 2017, 2018),
make(Mazda, BMW, Honda),
transmission(Automatic, Manual)

Just for test purpose I have put the all three Mazda posts under automatic transmission and 2016 year.
The problem is when I select the Mazda with manual transmission no result should be showing because it is only available for automatic filter. Same situation with other checkbox filter. There is something completely wrong and I need help to sort this out so the filter work properly.

This AJAX jQuery Code

var filter = $(‘#filter’);
data:filter.serialize(), // form data
type:filter.attr(‘method’), // POST
filter.find(‘button’).text(‘Applying Filters…’); },
filter.find(‘button’).text(‘Apply filters’);
return false;

This is form code

<form action=”<?php echo site_url() ?>/wp-admin/admin-ajax.php” method=”POST” id=”filter”>
if( $terms = get_terms( array(‘taxonomy’ => ‘filter’, ‘hide_empty’ => false) ) ) :
foreach ( $terms as $term ) :
if ($term->parent != 0) { // to avoid parent term
echo ‘<label for=”‘.$term->slug.'”><input type=”checkbox” id=”‘.$term->term_id.'”
name=”check_list[]” value=”‘.$term->slug.'”> ‘.$term->name.'</label>’;
<button>Apply filters</button>
<input type=”hidden” name=”action” value=”customfilter”>
<!– Display the result –>
<div id=”response”></div>

functions.php code

function my_filters(){

if( isset( $_POST[‘check_list’] ) )
$items = ($_POST[‘check_list’]);

$args[‘tax_query’] = array(
‘taxonomy’ => ‘filter’,
‘field’ => ‘slug’,
‘terms’ => $items

$query = new WP_Query( $args );

if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo ‘<h2>’ . $query->post->post_title . ‘</h2>’;
else :
echo ‘No posts found’;


add_action(‘wp_ajax_customfilter’, ‘my_filters’);
add_action(‘wp_ajax_nopriv_customfilter’, ‘my_filters’);

Read more here:: Post filter with multiple checkbox taxonomy terms

Leave a Reply

Your email address will not be published. Required fields are marked *