元查询关系“and”,然后相应地设置数组

时间:2014-11-17 作者:RMH

我在主页上有一个搜索表单,它为下面的代码设置相应的url query\\u字符串。当我使用或得到结果时。然而,当我对关系使用“AND”时,我没有得到任何结果,因为用户可以在搜索时选择他们想要选择的选项(查询中有三个选项,如下所示)

有人会建议如何修改查询,以便它必须使用“AND”来搜索准确的结果,但如果用户只使用搜索表单中的rating select下拉列表,那么它仍然会返回这些结果。而不是因为没有使用前两个选项而不显示任何内容。

形式:

<form id="resort_search_options" role="search" method="get" class="search-form form-inline" action="/test-results/">
          <input type="hidden" value="" name="Price" id="price" disabled>
          <input type="hidden" value="" name="Region" id="region" disabled>
          <input type="hidden" value="" name="Rating" id="rating" disabled>


          <div class="input-group">
            <select class="price">
              <option value="">Price</option>
              <option value="20">$20</option>
              <option value="500-1000">$500-1000</option>
              <option value="1500–2000">$1500–$2000</option>
              <option value="2000–2500">$2000–$2500</option>
              <option value="2500–3000">$2500–$3000</option>
              <option value="3000–3500">$3000–$3500</option>
              <option value="3500–4000">$3500–$4000</option>
              <option value="4000–4500">$4000–$4500</option>
              <option value="4500–5000">$4500–$5000</option>
              <option value="6500–7000">$6500–$7000</option>
              <option value="7000-9000">$7000-$9000</option>
              <option value="9500–10000">$9500–$10000</option>
              <option value="10000-30000">$10000-$30000</option>
              <option value="20000">$20000</option>
            </select>
            <select class="region">
              <option value="">Region</option>
              <option value="Place1">Place1</option>
              <option value="Place2">Place2</option>
              <option value="Place3">Place3</option>
              <option value="Place4">Place4</option>
              <option value="Place5">Place5</option>
            </select>
            <select class="rating">
              <option value="">Rating</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
            </select>

            <span class="input-group-btn">
              <button type="submit" class="search-submit btn btn-default"><?php _e(\'Search Resorts\', \'roots\'); ?></button>
            </span>
          </div>
        </form> 
结果:

<?php

      $queryPrice = explode(\'-\', $_GET[\'Price\']);
      $queryRegion = $_GET[\'Region\'];
      $queryRating = $_GET[\'Rating\'];


      $args = array(
          // all your args here
          \'post_type\'    => \'resorts\',
          \'meta_query\' => array(
              \'relation\' => \'AND\',
              array(
                  \'key\' => \'price\',
                  \'value\' => $queryPrice,
                  \'type\' => \'NUMERIC\',
                  // \'compare\' => \'BETWEEN\',
              ),
              array(
                  \'key\' => \'region\',
                  \'value\' => $queryRegion,
                  // \'compare\' => \'=\',
              ),
              array(
                  \'key\' => \'rating\',
                  \'value\' => $queryRating,
                  // \'compare\' => \'=\',
              ),

          )
      );

      $query = new WP_Query( $args );
      if($query->have_posts()) : while ($query->have_posts()): $query->the_post();


      ?>

stuff goes here, yay!

<?php endwhile; else : ?>
    <p>No results found, modify your search criteria and try again!</p>
    <?php endif; ?>
--

$meta_array = array();
      $meta_array = array( \'relation\' => \'AND\' );
      if( isset( $_POST[\'Price\'] ) $meta_array[] = array( //THIS IS LINE 46
          \'key\' => \'price\', 
          \'value\' => $Price, 
          \'compare\' => \'REGEX\' );
      if( isset( $_POST[\'Region\'] ) $meta_array[] = array( 
          \'key\' => \'region\', 
          \'value\' => $Region, 
          \'compare\' => \'=\' );
      if( isset( $_POST[\'Rating\']) $meta_array[] = array( 
          \'key\' => \'rating\',
          \'value\' => $Rating, 
          \'compare\' => \'REGEX\' );

      $args = array( 
              \'post_type\'         => \'resorts\',
              \'post_status\'       => \'publish\',
              \'meta_query\'        => $meta_array
          );

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

最终工作参数:

<?php


      if( isset( $_GET[\'Price\'] ) ) {
        $meta_array[] = 
          array(
              \'key\' => \'price\', 
              \'value\' => esc_attr($_GET[\'Price\'])
              // \'compare\' => \'=\' 

            );


      }

      if( isset( $_GET[\'Region\'] ) ) {
      $meta_array[] = 
        array(
            \'key\' => \'region\', 
            \'value\' => esc_attr($_GET[\'Region\'])
            // \'compare\' => \'=\' 
          );
      }

      if( isset( $_GET[\'Rating\'] ) ) {
        $meta_array[] =
          array(
              \'key\' => \'rating\', 
              \'value\' => esc_attr($_GET[\'Rating\'])
              // \'compare\' => \'=\' 

            );
      }

      $page_id = \'\';

      if( isset( $_GET[\'Title\'] ) ) {
        $page_id = esc_attr($_GET[\'Title\']);
      }


      $args = array( 
              \'post_type\'         => \'resorts\',
              \'page_id\' => $page_id,
              \'post_status\'       => \'publish\',
              \'orderby\' => \'title\',
              \'meta_query\'        => $meta_array
          );


      $args[\'meta_query\'][\'relation\'] = \'AND\';




      $query = new WP_Query( $args );

      if($query->have_posts()) : while ($query->have_posts()): $query->the_post();


      ?>

stuff happens

<?php endwhile; else : ?>
    <p>No results found, modify your search criteria and try again!</p>
    <?php endif; ?>

SO网友:Dongsan

我不确定我是否正确,但如果您试图通过多个元键进行查询,其中元键的数量不同,那么下面是我用于相同目的的内容。

基本上,如果存在相应的输入,它会一个接一个地添加元查询。

<form action="" method="post">
  <input name="item_name" />
  <select name="item_type">
    <option value="A">Item A</option>
    <option value="B">Item B</option>
    <option value="C">Item C</option>
  </select>
  <select name="company">
    <option value="D">Company D</option>
    <option value="E">Company E</option>
    <option value="F">Company F</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

<?php
$meta_array = array();
$meta_array = array( \'relation\' => \'AND\' );
if( isset( $_POST[\'item_name\'] ) ) $meta_array[] = array( \'key\' => \'_item_name\', \'value\' => $item_name, \'compare\' => \'REGEXP\' );
if( isset( $_POST[\'item_type\'] ) ) $meta_array[] = array( \'key\' => \'_item_type\', \'value\' => $item_type, \'compare\' => \'=\' );
if( isset( $_POST[\'company\'] ) ) $meta_array[] = array( \'key\' => \'_item_companies\', \'value\' => $allowed_company, \'compare\' => \'REGEXP\' );

$args = array( 
        \'post_type\'         => \'item\',
        \'post_status\'       => \'publish\',
        \'meta_query\'        => $meta_array
    );

$wp_query = new WP_Query( $args );



?>
当然,上述变量需要更改以满足您的需要。

希望这就是你想要的。

结束

相关推荐

JQuery Plugins in Wordpress

我已经能够在某种程度上拼凑出应该如何做到这一点,但我真的很难做到这一点。我想使用Table Sorter插件(http://tablesorter.com) 在自定义页面模板中显示数据,但我不确定它是否正确。我已经钩住了“wp\\u enqueue\\u scripts”,并使用此函数将表分类器JS文件排入队列。我相信这是正确的,但是我还需要在JQuery Ready()函数中放置一行,但是我不确定如何从自定义页面模板中执行此操作。有人能解释一下吗?<?php /* Templat