:ここ
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のであり、
これを達成するには、次のものが必要です。
グループによるグループ行動データ
(選択されたチェックボックス)に3210
- は、アレイの各グループに含める
'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',
),
),
),
);
だから、これはチェックボックスのグループで複数選択を可能にするために、期待通りに動作するはずです...