Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it not OK to accept a value from $_GET as the key in a WP Meta Query? I'm not sure why this code doesn't return any results...

BOTH the queries for Price as well as bedrooms do not return any results. Is my code incorrect? Any help will be much appreciated..

<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => 'property',
'paged' => $paged
);

if($_GET['beds'] >= 1) {
$args['meta_query'][] = array(
'key' => $_GET['beds'],
'value' => array('pyre_BHK-A','pyre_BHK-B','pyre_BHK-C','pyre_BHK-D','pyre_BHK-E'),
'compare' => 'IN'
);
}

if(($_GET['price-max'] >= 1)&&($_GET['price-min'] >= 1)) {
$args['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => 'pyre_price',
'value' => array($_GET['price-min'],$_GET['price-max']),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
array(
'key' => 'pyre_pricem',
'value' => array($_GET['price-min'],$_GET['price-max']),
'compare' => 'BETWEEN',
'type' => 'numeric'
)
);
}

query_posts($args);
if(have_posts()):
?>

<?php
while(have_posts()): the_post();
?>


<?php get_template_part( 'property-listing' );           // Navigation bar (property-listing.php) ?>

<?php endwhile; ?>

<?php else: ?>
<h3><?php echo of_get_option('search_results_none_title', 'No properties were found which match your search criteria.'); ?></h3>
<p><?php echo of_get_option('search_results_none_content', 'Try broadening your search to find more results.'); ?></p>
<?php endif; ?>
share|improve this question
    
what are the keys which you are getting using $_GET and why you are comparing it using >= int –  Anjum Sep 5 '13 at 9:14
    
@Anjum I'm getting the number of bedrooms ($_GET['beds']) and maximum price ($_GET['price-max']) and minimum price ($_GET['price-min']) from the user. I never used >= where did you see that? I'm only comparing using 'IN' and 'BETWEEN' –  kuchikoo Sep 5 '13 at 10:31
add comment

1 Answer 1

You have some errors on how meta_query array is setted for price-max and price-min.

Also in your code will be an issue if no $_GET['beds'] are sended but $_GET['price-min'] and $_GET['price-max'] are. this because the $args['meta_query'] array is setted inside if($_GET['beds'] >= 1).

Moreover your code will throw a lot of notices if the GET are not sended, you have to check if $_GET variables are set before using and, in addition, you should ensure them are numeric values if you want to use as numeric.

Finally, query_posts should always be avoided, use WP_Query instead.

This code should work, but is untested:

<?php
if ( ! is_front_page() ) {
  $paged = get_query_var('paged') ? : 1;
} else {
  $paged = get_query_var('page') ? : 1;
}

$beds = isset($_GET['beds']) && intval($_GET['beds']) ? $_GET['beds'] : 0;
$max = isset($_GET['price-max']) && intval($_GET['price-max']) ? $_GET['price-max'] : 0;
$min = isset($_GET['price-min']) && intval($_GET['price-min']) ? $_GET['price-min'] : 0;

$args = array(
  'post_type' => 'property',
  'paged' => $paged
);

if( $beds >= 1) {
  $args['meta_query'][] = array(
    'key' => $beds, // are you sure for that?
    'value' => array('pyre_BHK-A','pyre_BHK-B','pyre_BHK-C','pyre_BHK-D','pyre_BHK-E'),
    'compare' => 'IN'
  );
}

if( ($max >= 1) && ($min) ) {
  if ( ! isset($args['meta_query']) ) $args['meta_query'] = array();
  $args['meta_query']['relation'] = 'OR';
  $args['meta_query'][] = array(
    'key' => 'pyre_price',
    'value' => array($min, $max),
    'compare' => 'BETWEEN',
    'type' => 'numeric'
  );
  $args['meta_query'][] = array(
    'key' => 'pyre_pricem',
    'value' => array($min, $max),
    'compare' => 'BETWEEN',
    'type' => 'numeric'
  );
}

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

get_template_part( 'property-listing' );
// Navigation bar (property-listing.php)

endwhile;

else:
?>  
<h3><?php echo of_get_option('search_results_none_title', 'No properties were found which match your search criteria.'); ?></h3>
<p><?php echo of_get_option('search_results_none_content', 'Try broadening your search to find more results.'); ?></p>
<?php
wp_reset_postdata();
endif;
?>
share|improve this answer
    
+1 from me, it's exactly what i want to say, but i was gone to meet a friend and now i am ... –  Anjum Sep 5 '13 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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