Skip to content

Instantly share code, notes, and snippets.

@webhasan
Last active January 25, 2018 18:26
Show Gist options
  • Save webhasan/e8f7f4478c970f0887e5d5b39206b828 to your computer and use it in GitHub Desktop.
Save webhasan/e8f7f4478c970f0887e5d5b39206b828 to your computer and use it in GitHub Desktop.
WordPress Advance Search
<?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