【问题标题】:Checkbox filed not filtering form acf fields复选框字段未过滤表单 acf 字段
【发布时间】:2018-02-16 05:41:32
【问题描述】:

我使用过 ACF 复选框字段。现在我要过滤查询是这个复选框字段。然后我试图从前端搜索。但它没有显示任何结果。字段 meta_key 是 cotfacf 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
    );

【问题讨论】:

    标签: php wordpress filtering advanced-custom-fields meta


    【解决方案1】:

    你的代码应该是这样的。

    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'
                )            
            ));
        }
    }
    

    【讨论】:

    • 感谢@vel,但它显示此错误Warning: strtoupper() expects parameter 1 to be string, array given in C:\wamp64\www\verde\wp-includes\class-wp-meta-query.php on line 131
    • Warning: strtoupper() expects parameter 1 to be string, array given in C:\wamp64\www\verde\wp-includes\class-wp-meta-query.php on line 131
    • 我没有在我的代码中使用这个函数strtoupper()。检查你的代码
    • 打印这个值并检查。 $_POST['ap_features']。是字符串还是数组?
    • print_r 结果Array ( [0] =&gt; business_center [1] =&gt; clubhouse )
    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多