具有多个数字的Meta_Query问题

时间:2015-11-03 作者:benjosity

我对搜索表单有问题。我基本上有7个用于搜索不同属性的输入字段。除了最后两个之外,所有这些都是有效的,只要我介绍最后两个(最小价格和最大价格),表单就不会返回任何结果,而如果我把它们从等式中取出来,表单就会很好地工作。

查询如下所示-

<?php
    $_location = $_GET[\'location\'] != \'\' ? $_GET[\'location\'] : \'\';
    $_status = $_GET[\'status\'] != \'\' ? $_GET[\'status\'] : \'\';
    $_type = $_GET[\'type\'] != \'\' ? $_GET[\'type\'] : \'\';
    $_minbed = $_GET[\'minbed\'] != \'\' ? $_GET[\'minbed\'] : \'\';
    $_maxbed = $_GET[\'maxbed\'] != \'\' ? $_GET[\'maxbed\'] : \'\';
    $_minprice = $_GET[\'minpay\'] != \'\' ? $_GET[\'minpay\'] : \'\';
    $_maxprice = $_GET[\'maxprice\'] != \'\' ? $_GET[\'maxprice\'] : \'\';

    // Start the Query
    $property_args = array(
      \'post_type\'     =>  \'property-spaces\',
      \'posts_per_page\' => -1,
      \'meta_query\'    =>  array(
        array(
          \'key\'     => \'wpcf-location-area\',
          \'value\'   => $_location,
          \'compare\' => \'LIKE\',
          ),
        array(
          \'key\'     => \'wpcf-property-status\',
          \'value\'   => $_status,
          \'compare\' => \'LIKE\',
          ),
        array(
          \'key\'     => \'wpcf-property-type\',
          \'value\'   => $_type,
          \'compare\' => \'LIKE\',
          ),
        array(
          \'key\' => \'wpcf-total-rooms\',
          \'value\' => $_minbed,
          \'compare\' => \'>=\',
          \'type\' => \'numeric\',
          ),
        array(
          \'key\' => \'wpcf-total-rooms\',
          \'value\' => $_maxbed,
          \'compare\' => \'<=\',
          \'type\' => \'numeric\',
          ),
        array(
          \'key\' => \'wpcf-min-room-price\',
          \'value\' => $_minprice,
          \'compare\' => \'>=\',
          \'type\' => \'numeric\',
          ),
        array(
          \'key\' => \'wpcf-max-room-price\',
          \'value\' => $_maxprice,
          \'compare\' => \'<=\',
          \'type\' => \'numeric\',
          ),
      )
    );
    $propertySearchQuery = new WP_Query( $property_args );
    if( $propertySearchQuery->have_posts() ) :
    ?>
这对我来说是一个新领域,所以我有点迷茫,我猜这与元查询有关,因为我已经检查了表单中的所有变量和名称。

1 个回复
最合适的回答,由SO网友:jdm2112 整理而成

显然是未测试的半伪代码。其思想是,元查询已经是一个数组数组,表单中的每个字段都是另一个嵌套数组。如果存在一个值,我们将其推送到数组中。

<?php
$_location = $_GET[\'location\'] != \'\' ? $_GET[\'location\'] : \'\';
$_status = $_GET[\'status\'] != \'\' ? $_GET[\'status\'] : \'\';
// Etc for all form fields

$meta_query = array(); // Declare empty array


// Test each var for a value; if exists add to meta query array
if($_location) $meta_query[] = array( \'key\' => \'wpcf-location-area\', \'value\' => $_location, \'compare\' => \'LIKE\' );
if($_status) $meta_query[] = array( \'key\' => \'wpcf-property-status\', \'value\'   => $_status, \'compare\' => \'LIKE\' );

// Start the Query
$property_args = array(
  \'post_type\'     =>  \'property-spaces\',
  \'posts_per_page\' => -1,
  \'meta_query\'    =>  $meta_query
);

$propertySearchQuery = new WP_Query( $property_args );

相关推荐

Slow Query On Search

请注意,我在wordpress搜索中有一个非常慢的查询,使用SHOW FULL PROCESSLIST; 下面是一个查询示例SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE \'%Industreet%\') OR (wp_posts.post_excerpt LIKE \'%Industreet%\') OR (wp_posts.post_content LI