1

I'm having an issue with a search form. I basically have 7 input fields used to search different properties. All of them apart from the last two see to be working, as soon as I introduce the last two (min price & max price) the form returns no results whereas if I take them out the equation the form works fine.

The query looks like this -

<?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() ) :
    ?>

This is new territory for me so I'm a bit lost, I'm guessing it's something to do with the meta query as I've gone through and checked all variables and the names within the form.

7
  • Your query logic is such that every meta condition must pass for any result to be returned. Is that your intent?
    – jdm2112
    Nov 3, 2015 at 13:50
  • To add to jdm2112's comment, you can add a 'relation'=>'OR' as the first item of your meta_query array (by default the relation is 'AND')
    – Sladix
    Nov 3, 2015 at 14:19
  • @jdm2112 I need it so each of those meta conditions are passed, the form that's being used has default values if the user doesn't input anything. Adding 'relation'=>'OR' just seems to return all the properties no matter what search criteria I use.
    – benjosity
    Nov 3, 2015 at 14:40
  • Personally I would test if each field has a value and if it does concat to your meta query. If all fields are empty, you would want to test for that as well to prevent dumping all of your data as a result. I'll post an answer to show what I'm thinking.
    – jdm2112
    Nov 3, 2015 at 15:00
  • Never ever use unsanitized values from a GET variable. It is extremely dangerous as this is the easiest spot hackers use to inject malicious code into your site. ALWAYS sanitize, escape and validate and never trust any input values, not even your own Nov 3, 2015 at 19:25

1 Answer 1

0

Untested and semi-pseudo code obviously. The idea is that a meta query is already an array of arrays, each field in your form is another nested array. If a value exists we push it onto the array.

<?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 );
2
  • Thankyou! That did the trick. I did upvote you but with me being a rookie it won't display it.
    – benjosity
    Nov 4, 2015 at 10:22
  • Glad it worked for you.
    – jdm2112
    Nov 4, 2015 at 14:49

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.