Last active
January 25, 2018 18:26
-
-
Save webhasan/e8f7f4478c970f0887e5d5b39206b828 to your computer and use it in GitHub Desktop.
WordPress Advance Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$query = $_POST['query']; | |
$price = $_POST['price']; | |
$rooms = $_POST['rooms']; | |
$bedrooms = $_POST['bedrooms']; | |
$size = $_POST['size']; | |
$args = array('post_type' => 'post'); | |
if(!empty($query)) { | |
$args['s'] = $query; | |
} | |
if(!empty($price)) { | |
$min = explode('-', $price)[0]; | |
$max = explode('-', $price)[1]; | |
$args['meta_query'][] = | |
array( | |
'key' => 'price', | |
'value' => array( $min, $max), | |
'type' => 'numeric', | |
'compare' => 'BETWEEN', | |
); | |
} | |
if(!empty($rooms)) { | |
$args['meta_query'][] = | |
array( | |
'key' => 'rooms', | |
'value' => $rooms, | |
'type' => 'numeric', | |
'compare' => '=', | |
); | |
} | |
if(!empty($size)) { | |
$args['meta_query'][] = | |
array( | |
'key' => 'size', | |
'value' => $size, | |
'type' => 'numeric', | |
'compare' => '=', | |
); | |
} | |
if(!empty($bedrooms)) { | |
$args['meta_query'][] = | |
array( | |
'key' => 'bedroom', | |
'value' => $bedrooms, | |
'type' => 'numeric', | |
'compare' => '=', | |
); | |
} | |
$the_query = new WP_Query($args); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment