1

I have a search form on my home page that sets up the url query_string accordingly for the code below. When I use or I get results. However when I used "AND" for the relation I dont get any results as the user can select as many of the options as they want to choose from when searching (there are three as seen below in the query)

How would anyone suggest altering the query so that it has to search for the exact results using "AND", but say the user only uses the rating select dropdown in the search form, then it would still return those results. As opposed to not showing anything because the first 2 choices weren't being used.

form:

<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> 

results:

<?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 Answers 2

0

I'm not sure if I get you right, but if you're trying to query by multiple meta keys where the number of meta keys varies, then below is what I am using for the same purpose.

Basically it adds meta query one after another if the corresponding input is existent.

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



?>

Of course the variables above need to be changed to suit your needs.

Hope this was what you were looking for.

2
  • i've added the following, see above please: and its giving me this error: Parse error: syntax error, unexpected '$meta_array' (T_VARIABLE) in /home/fiji/public_html/wp-content/themes/fiji_roots/page-test-results.php on line 46
    – RMH
    Commented Nov 18, 2014 at 3:14
  • RMH, sorry I missed to close the parenthesis. if( isset( $_POST['Price'] ) should be if( isset( $_POST['Price'] ) ), I believe.
    – Dongsan
    Commented Nov 20, 2014 at 10:10
0

The final working arguments:

<?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; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.