dongyuqie4322 2016-09-02 16:10
浏览 67
已采纳

在AJAX后置过滤器函数中使用多个“关系”参数

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

  • 写回答

1条回答 默认 最新

  • dongzhong9055 2016-09-04 03:34
    关注

    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…

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用