我正在尝试使用通过复选框组使用多选的表单来实现ajax后置过滤器。
此过滤器包含 5组(主密钥) brand
, ram
, camera
, price
和 feature
。每个组都有4到5个不同的键/值(复选框)。
目前,这仅适用于单选模式: 如果我选择了同一组的2个复选框,则不显示任何内容......
如何为这组复选框启用多项选择?
这是进行Ajax查询的php函数:
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();
}
?>
以下是此主题中使用的html表单和javascript:
Get posts with Ajax posts filter with multi selection checkboxes
由于
答案 0 :(得分:1)
要实现这一目标,您需要:
- 按组(选中的复选框)
对操作数据进行分组- 在每个数组中包含
'relation' => 'OR'
(仅,如果该组选中了多个复选框)
你的php函数将是这样的:
add_action('wp_ajax_call_post', 'call_post');
add_action('wp_ajax_nopriv_call_post', 'call_post');
function call_post(){
$choices = $_POST['choices'];
// Defining here your fields groups
$groups = array('brand', 'ram', 'camera', 'price', 'feature');
// Grouping data by group
foreach($choices as $Key => $Value){
foreach ($Value as $Inkey => $Invalue) {
switch ($Key) {
// One block for each group defined in $groups array
case $groups[0]:
$grp[0][] = array( 'key' => $Key, 'value' => $Invalue, 'compare' => 'like' );
break;
case $groups[1]:
$grp[1][] = array( 'key' => $Key, 'value' => $Invalue, 'compare' => 'like' );
break;
case $groups[2]:
$grp[2][] = array( 'key' => $Key, 'value' => $Invalue, 'compare' => 'like' );;
break;
case $groups[3]:
$grp[3][] = array( 'key' => $Key, 'value' => $Invalue, 'compare' => 'like' );
break;
case $groups[4]:
$grp[4][] = array( 'key' => $Key, 'value' => $Invalue, 'compare' => 'like' );
break;
}
}
}
$grp_arr = array();
// Adding ('relation' => 'OR') to each group with a length > 1
foreach ($grp as $key_grp => $grp_values) {
if(count($grp_values) > 1){
$grp_arr[$key_grp] = array('relation' => 'OR');
}
foreach ($grp_values as $grp_val) {
$grp_arr[$key_grp][] = $grp_val;
}
}
// Compiling it all
$meta_query = array('relation' => 'AND');
foreach ($grp_arr as $grp_arr_val) {
$meta_query[] = $grp_arr_val;
}
// The query (compiled)
$query = new WP_Query( array(
'post_type' => 'post',
'meta_query' => $meta_query
) );
// The Loop
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();
}
所以你会得到这种格式化的数组:
$query = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'brand',
'value' => 'Nokia',
'compare' => 'like',
),
array(
'key' => 'brand',
'value' => 'LG',
'compare' => 'like',
),
),
array(
'relation' => 'OR',
array(
'key' => 'ram',
'value' => '1GB',
'compare' => 'like',
),
array(
'key' => 'ram',
'value' => '2GB',
'compare' => 'like',
),
),
),
);
所以这应该按预期工作,以便通过一组复选框启用多选...