php - 在 AJAX 后置过滤器函数中使用多个 'relation' 参数

标签 php html arrays ajax wordpress

我正在尝试使用通过复选框组使用多选 的表单来实现 ajax 后置过滤器。

这个过滤器有5组(主键)是brand, ram相机价格功能 .每个组都有 4 到 5 个不同的键/值(复选框)。

For the moment, this works in mono-selection mode only: If I chose 2 checkboxes of the same group, nothing is displayed…

如何为这组复选框启用多选?

这是进行 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

谢谢

最佳答案

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)

你的 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',
            ),
        ),
    ),
);

所以这应该按预期工作,以启用复选框组的多选......

关于php - 在 AJAX 后置过滤器函数中使用多个 'relation' 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296734/

相关文章:

c++ - 将二维数组和字符串 vector 作为参数传递给函数

php - 在 Chrome 中调试以查找创建特定 HTML 的 PHP 文件

php - 有没有办法在 <fb :like> is clicked? 之后重新加载页面

arrays - 检查元素是否存在于 Bash 数组中

javascript - 选择 html 元素文本作为 JavaScript 的输入

javascript - 根据用户文本输入更新 url 链接并在 Wordpress 中保存更改

arrays - 创建 TPair TArray 的编译器错误

php - Laravel 获取相关模型的相关模型

PHPExcel_IOFactory::createWrite() 中的 PHPExcel 命名空间问题

html - HTML标签名称: "<TD>"的由来是什么