按元值搜索显示不正确的结果

时间:2014-03-07 作者:Dk-Macadamia

我正在按meta value进行自定义搜索,并使用以下内容:

print_r($_REQUEST);
$args = array(
    \'post_type\' => \'property_post\',
    \'posts_per_page\' => 10,
    \'order\' => \'ASC\',
    \'meta_query\' => array(
                     \'relation\' => \'OR\',
                     array(
                      \'key\' => \'custom_textarea\',
                      \'value\' => \'me\',  // if I use static keyword it works
                      \'compare\' => \'LIKE\'
                      ),
                      array(
                      \'key\' => \'custom_price\',
                      \'value\' => array( $_REQUEST[\'custom_price\'], $_REQUEST[\'custom_price1\'] ),
                      \'type\' => \'numeric\',
                      \'compare\' => \'BETWEEN\'
                      ),
                      array(
                      \'key\' => \'custom_beds\',
                      \'value\' => $_REQUEST[\'custom_beds\'],
                      \'compare\' => \'=\'
                      ),
                      array(
                      \'key\' => \'custom_bath\',
                      \'value\' => $_REQUEST[\'custom_bath\'],
                      \'compare\' => \'=\'
                      ),
                      array(
                      \'key\' => \'custom_garage\',
                      \'value\' => $_REQUEST[\'custom_garage\'],
                      \'compare\' => \'=\'
                      )
                      )

);
如果我对meta值使用一些静态关键字,那么它可以工作,但是$_REQUEST 没有,我查过了$_REQUEST 使用print_r($_REQUEST):

Array ( [custom_textarea] => aa[custom_price] => 1000 [custom_price1] => 4000[custom_beds] => 2[custom_garage] => 1)
那么我该怎么做才能让它工作呢?

3 个回复
SO网友:gmazzap

首先,一个人永远不应该依赖原始的$_REQUEST: 相信我,这是一个安全问题。

PHP有2个功能:filter_inputfilter_input_array 这有助于清理请求数据。

此外,在您的请求转储中,我没有看到任何$_REQUEST[\'custom_bath\'], 但您正在使用该值,这可能是问题的原因。在使用变量之前,应该检查它是否已设置。

使用我的建议,您的代码会变成这样

$args = array(
  \'custom_textarea\'   => FILTER_SANITIZE_STRING,
  \'custom_price\'      => FILTER_VALIDATE_INT,
  \'custom_price1\'     => FILTER_VALIDATE_INT,
  \'custom_beds\'       => FILTER_SANITIZE_STRING,
  \'custom_bath\'       => FILTER_SANITIZE_STRING,
  \'custom_garage\'     => FILTER_SANITIZE_STRING
);

$get = filter_input_array( INPUT_GET, $args, TRUE );
$post = filter_input_array( INPUT_POST, $args, TRUE );
$request = array_merge( $get, $post );

$query_args = array( \'relation\' => \'OR\' );

if ( ! empty( $request[\'custom_textarea\'] ) ) {
   $query_args[] = array(
     \'key\' => \'custom_textarea\',
     \'value\' => "%{$request[\'custom_textarea\']}%", 
     \'compare\' => \'LIKE\'
   );
}

if ( ! empty( $request[\'custom_price\'] ) && ! empty($request[\'custom_price1\']) ) {
   $query_args[] = array(
     \'key\' => \'custom_price\',
     \'value\' => array( "{$request[\'custom_price\']}", "{$request[\'custom_price1\']}" ), 
     \'compare\' => \'BETWEEN\',
     \'type\'    => \'SIGNED\'
   );
}

if ( ! empty( $request[\'custom_beds\'] ) ) {
   $query_args[] = array(
     \'key\' => \'custom_beds\',
     \'value\' => "{$request[\'custom_beds\']}", 
   );
}

if ( ! empty( $request[\'custom_bath\'] ) ) {
   $query_args[] = array(
     \'key\' => \'custom_bath\',
     \'value\' => "{$request[\'custom_bath\']}", 
   );
}   

if ( ! empty( $request[\'custom_garage\'] ) ) {
   $query_args[] = array(
     \'key\' => \'custom_garage\',
     \'value\' => "{$request[\'custom_garage\']}", 
   );
} 

$query = new WP_Query( $query_args );  

SO网友:sri

问题是,当您有一个字符串变量Wordpress时,它不会自动向该变量添加引号,因此搜索过滤器不会生成任何结果。

我建议以下答案。

$custom_textarea = \'\\\'\'.$_REQUEST[\'custom_textarea\'].\'\\\'\';  // declare a variable something like this.
然后你的meta_query 将是:

\'meta_query\' => array(
                     \'relation\' => \'OR\',
                     array(
                      \'key\' => \'custom_textarea\',
                      \'value\' => $custom_textarea, 
                      \'compare\' => \'LIKE\'
                      ),
                      array(
                      \'key\' => \'custom_price\',
                      \'value\' => array( $_REQUEST[\'custom_price\'], $_REQUEST[\'custom_price1\'] ),
                      \'type\' => \'numeric\',
                      \'compare\' => \'BETWEEN\'
                      ),
                      array(
                      \'key\' => \'custom_beds\',
                      \'value\' => $_REQUEST[\'custom_beds\'],
                      \'compare\' => \'=\'
                      ),
                      array(
                      \'key\' => \'custom_bath\',
                      \'value\' => $_REQUEST[\'custom_bath\'],
                      \'compare\' => \'=\'
                      ),
                      array(
                      \'key\' => \'custom_garage\',
                      \'value\' => $_REQUEST[\'custom_garage\'],
                      \'compare\' => \'=\'
                      )
                      )

SO网友:user43251

对于我的,我在搜索结果页面上也是这样使用的:

$pricemax = $_POST[\'price\'];
然后在查询中。。。

array(
        \'key\' => \'custom_rent\',
        \'value\' => array(\'\', $pricemax),
        \'compare\' => \'BETWEEN\',
        \'type\' => \'NUMERIC\'
        ),
这有帮助吗?是否尝试使用$\\u POST而不是$\\u请求?

结束

相关推荐

Search with filters and title

我想搜索custom_post 按标题和ACF字段。所以,我用了WP_Query WordPress函数,但我不能按标题过滤,只能按过滤器过滤。当我提交表单时,我有这样的URL:http://example.com/?s=titre&filter1=condition1&filter2=condition2&filter3=condition3 我的代码:$title = $_GET[\'s\']; $args = array( \'pagenam