代码之家  ›  专栏  ›  技术社区  ›  jays

复选框字段未筛选表单acf字段

  •  0
  • jays  · 技术社区  · 7 年前

    我使用了ACF复选框字段。现在我想过滤查询是这个复选框字段。然后我试着从前端搜索。但它没有显示任何结果。字段meta\u键为 cotf . acf fields screenshot 1

    acf fields screenshot 2

    <form action="" id="" method="POST">
    <input type="checkbox" name="ap_features[]" value="air_conditioning">
    <input type="checkbox" name="ap_features[]" value="washer_connections">
    <input type="checkbox" name="ap_features[]" value="refrigerator">
    <input  class="btn btn-lg  btn-success" type='submit' id="" name="submit" value="SEARCH">
    </form>
    

    PHP

    <?php
        if(isset($_POST['submit'])){
            $afeatures = $_POST['ap_features'];
            $args = array(  
                'numberposts'   => -1,
                'post_type'     => 'apartment',
                'meta_query'    => array(
                'relation'      => 'AND',
                    array(
                        'key'       => 'cotf',
                        'value' => array($afeatures),
                        'compare'   => 'IN'
                    )
                )
            );
        }
    ?>
    

    我也尝试过此代码,但它不起作用:

    $meta_query = array('relation' => 'OR');
        foreach ($_POST['ap_features'] as $value) {
          $meta_query[] = array(
          'key' => 'cotf',
          'value' => '"'.$value.'"',
          'compare' => 'LIKE'
          )
        }
        $args = array(
          'numberposts' => -1,
          'post_type' => 'restaurant',
          'meta_query' => $meta_query
        );
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Vel    7 年前

    您的代码应该是这样的。

    if(isset($_POST['submit'])){
    
        $args = array(  
            'numberposts'   => -1,
            'post_type'     => 'apartment',            
        );
        $afeatures = $_POST['ap_features'];
    
        if(is_array($afeatures)){
            $afeaturesArr = implode(",",$afeatures);
    
            $args['meta_query']= array(
            'relation'=>array('AND',
                array(
                'key'       => 'cotf',
                'value' => explode(",",$afeaturesArr),
                'compare'   => 'IN'
                )            
            ));
        }
    }