在AJAX后期过滤器函数中使用多个“关系”参数 [英] Use multiple 'relation' arguments in AJAX post filter function
问题描述
我试图通过复选框组使用多选的形式来实现ajax后期过滤器。
此过滤器有 品牌
, ram $ <5>
c $ c> , 相机
, 价格
和 功能
。每个组都有4到5个不同的键/值(复选框)。
目前,这可用于单选模式:
如果我选择了同一组的两个复选框,不显示任何内容...
如何为这组复选框启用多选?
以下是使用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($ options as $ Key => $ Value){
if(count($ Value)){
foreach($ Value as $ Inkey => $ Invalue){
$ meta_query [] = array('key'=> $ Key,'value'=> $ Invalue,'compare'=>'like');
$ b $ 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('抱歉,没有帖子符合您的标准。');
wp_send_json($ query-> posts);
endif;
die();
}
?>
以下是在此线程中使用的html表单和javascript:
使用Ajax帖子过滤器获取帖子与多选择复选框
谢谢
为了实现这一点,您需要:
- 按组将组动作数据(选定的复选框)
>
- 包含在每个数组中
'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'];
//定义您的字段组
$ groups = array('brand','ram','camera','price','feature');
//按组分组数据
foreach($ options as $ Key => $ Value){
foreach($ Value as $ Inkey => $ Invalue){
switch($ Key){
// $ groups array
$ b $ case $ groups [0]中定义的每个组的一个块:
$ grp [0] [] = array('key'=> $ Key,'value'=> $ Invalue,'compare'=>'like');
休息;
$ b $ case $ groups [1]:
$ grp [1] [] = array('key'=> $ Key,'value'=> $ Invalue,'compare' =>'like');
休息;
$ b $ case $ groups [2]:
$ grp [2] [] = array('key'=> $ Key,'value'=> $ Invalue,'compare' =>'like');;
休息;
$ b $ case $ groups [3]:
$ grp [3] [] = array('key'=> $ Key,'value'=> $ Invalue,'compare' =>'like');
休息;
$ b $ case $ groups [4]:
$ grp [4] [] = array('key'=> $ Key,'value'=> $ Invalue,'compare' =>'like');
休息;
}
}
}
$ grp_arr = array();
//将('relation'=>'OR')添加到长度> 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;
//编译全部
$ meta_query = array('relation'=&'; AND');
foreach($ grp_arr as $ grp_arr_val){
$ meta_query [] = $ grp_arr_val;
查询(编译后)
$ query = new WP_Query(array(
'post_type'=>'post',
'meta_query'=> $ meta_query
)); ($ query-> have_posts()):$ query-> the_post ();
get_template_part('content');
endwhile;
wp_reset_query();!
else:
_e('抱歉,没有帖子符合您的标准。');
wp_send_json($ query-> posts);
endif;
die();
}
所以你会得到这种格式化的数组:
$ query = array(
'post_type'=>'product',
'meta_query'=> array(
'relation'=>'AND',
array(
'relation'=>'OR',
array(
'key'=>' '
'value'=>'Nokia',
'compare'=>'like',
),
array(
'key' ='品牌',
'值'=>'LG',
'比较'=>'like',
),
),
数组(
'relation'=>'OR',
array(
'key'=>'ram',
'value'=>'1GB' ,
'比较'=>'之类',
),
数组(
'key'=>'ram',
'value'=> '2GB',
'compare'=> 'b'),
),
),
);
所以这应该按预期工作,以便通过一组复选框进行多选...
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
To achieve this you need:
- to group actions data by groups (selected checkboxes)
- to include in each group of arrays a
'relation' => 'OR'
(only if the group has more than one check-box selected)
Your php function will be like this:
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();
}
So you will get this kind of formatted array:
$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',
),
),
),
);
So This should work as expected, to enable multi-selection by group of checkboxes…
这篇关于在AJAX后期过滤器函数中使用多个“关系”参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!