I am trying to achieve an ajax post filter with a form which use muti-selection through groups of checkboxes.
This filter have 5 groups (master keys) which are brand
, ram
, camera
, price
and feature
. Every group have between 4 and 5 different key / value (checkboxes).
For the moment, this works in mono-selection mode only: If I chose 2 checkboxes of the same group, nothing is displayed…
How can I enable multi-selection for this groups of checkboxes?
Here is the php function that make the Ajax query:
add_action('wp_ajax_call_post', 'call_post');
add_action('wp_ajax_nopriv_call_post', 'call_post');
function call_post(){
$choices = $_POST['choices'];
$meta_query = array('relation' => 'AND');
foreach($choices as $Key=>$Value){
if(count($Value)){
foreach ($Value as $Inkey => $Invalue) {
$meta_query[] = array( 'key' => $Key, 'value' => $Invalue, 'compare' => 'like' );
}
}
}
$args = array(
'post_type' => 'post',
'meta_query' =>$meta_query
);
$query = new WP_Query($args);
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
get_template_part('content');
endwhile;
wp_reset_query();
else :
_e('Sorry, no posts matched your criteria.');
wp_send_json($query->posts);
endif;
die();
}
?>
Here are the html form and the javascript, that are used in this thread:
Get posts with Ajax posts filter with multi selection checkboxes
Thanks