Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active December 28, 2015 00:09
Show Gist options
  • Save yratof/7410728 to your computer and use it in GitHub Desktop.
Save yratof/7410728 to your computer and use it in GitHub Desktop.
Property Search
<?php
/*
* Template Name: Property Search
*/
get_header();
if(isset($_GET['propertysearch'])) {
$property_areas = $_GET['property_areas'];
$property_type = $_GET['property_type'];
$town = $_GET['town'];
$min_bedrooms = $_GET['min_bedrooms'];
$max_bedrooms = $_GET['max_bedrooms'];
$rent_buy = $_GET['rent_buy'];
$rent_min_price = $_GET['rent_min_price'];
$rent_max_price = $_GET['rent_max_price'];
$buy_min_price = $_GET['buy_min_price'];
$buy_max_price = $_GET['buy_max_price'];
}
//Build the Search Array
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$search_args = array();
$search_args['post_type'] = 'property';
$search_args['posts_per_page'] = 5;
$search_args['paged'] = $paged;
$search_args['tax_query'] = array(
array(
'taxonomy' => 'area',
'field' => 'slug',
'terms' => $property_areas
)
);
$search_args['meta_query'] = array();
if($property_type > 0) {
$search_args['meta_query'][] = array(
'key' => '_property_type',
'value' => $property_type,
'compare' => '='
);
}
if($town != 0) {
$search_args['meta_query'][] = array(
'key' => '_town',
'value' => $town,
'compare' => '='
);
}
if($min_bedrooms > 0 && $max_bedrooms > 0) {
$search_args['meta_query'][] = array(
'key' => '_number_bedrooms',
'value' => array( $min_bedrooms, $max_bedrooms ),
'type' => 'numeric',
'compare' => 'BETWEEN'
);
} elseif($min_bedrooms > 0 && $max_bedrooms == 0) {
$search_args['meta_query'][] = array(
'key' => '_number_bedrooms',
'value' => $min_bedrooms,
'type' => 'numeric',
'compare' => '>='
);
} elseif($min_bedrooms == 0 && $max_bedrooms > 0) {
$search_args['meta_query'][] = array(
'key' => '_number_bedrooms',
'value' => $max_bedrooms,
'type' => 'numeric',
'compare' => '<='
);
}
if($rent_buy == 'rent') {
if($rent_min_price > 0 && $rent_max_price > 0) {
$search_args['meta_query'][] = array(
'key' => '_rental_sale_value',
'value' => array( $rent_min_price, $rent_max_price ),
'type' => 'numeric',
'compare' => 'BETWEEN'
);
} elseif($rent_min_price > 0 && $rent_max_price == 0) {
$search_args['meta_query'][] = array(
'key' => '_rental_sale_value',
'value' => $rent_min_price,
'type' => 'numeric',
'compare' => '>='
);
} elseif($rent_min_price == 0 && $rent_max_price > 0) {
$search_args['meta_query'][] = array(
'key' => '_rental_sale_value',
'value' => $rent_max_price,
'type' => 'numeric',
'compare' => '<='
);
}
} elseif($rent_buy == 'buy') {
if($buy_min_price > 0 && $buy_max_price > 0) {
$search_args['meta_query'][] = array(
'key' => '_rental_sale_value',
'value' => array( $buy_min_price, $buy_max_price ),
'type' => 'numeric',
'compare' => 'BETWEEN'
);
} elseif($buy_min_price > 0 && $buy_max_price == 0) {
$search_args['meta_query'][] = array(
'key' => '_rental_sale_value',
'value' => $buy_min_price,
'type' => 'numeric',
'compare' => '>'
);
} elseif($buy_min_price == 0 && $buy_max_price > 0) {
$search_args['meta_query'][] = array(
'key' => '_rental_sale_value',
'value' => $buy_max_price,
'type' => 'numeric',
'compare' => '<'
);
}
}
//
$_SESSION['orderby'] = $_POST['order_by'];
if(isset($_SESSION['orderby'])) {
if($_SESSION['orderby'] == "price_high") {
$search_args['meta_key'] = '_rental_sale_value';
$search_args['orderby'] = 'meta_value_num';
$search_args['order'] = 'DESC';
} elseif($_SESSION['orderby'] == "price_low") {
$search_args['meta_key'] = '_rental_sale_value';
$search_args['orderby'] = 'meta_value_num';
$search_args['order'] = 'ASC';
} elseif($_SESSION['orderby'] == "bedrooms_high") {
$search_args['meta_key'] = '_number_bedrooms';
$search_args['orderby'] = 'meta_value_num';
$search_args['order'] = 'DESC';
} elseif($_SESSION['orderby'] == "bedrooms_low") {
$search_args['meta_key'] = '_number_bedrooms';
$search_args['orderby'] = 'meta_value_num';
$search_args['order'] = 'ASC';
}
}
// the query
$search_query = new WP_Query( $search_args );
echo '<div id="page_container">';
if ( $search_query->have_posts() ) {
echo '<div class="order_by_container">';
echo '<form id="order_by_form" method="post" action="">';
echo '<label for="order_by">Order by </label>';
echo '<select class="order_by" name="order_by">';
echo '<option value="0" ';
if($_SESSION['orderby'] == 0) {
echo 'selected';
}
echo '>None</option>';
echo '<option value="price_high" ';
if($_SESSION['orderby'] == "price_high") {
echo 'selected';
}
echo '>Price High > Low</option>';
echo '<option value="price_low" ';
if($_SESSION['orderby'] == "price_low") {
echo 'selected';
}
echo '>Price Low > High</option>';
echo '<option value="bedrooms_high" ';
if($_SESSION['orderby'] == "bedrooms_high") {
echo 'selected';
}
echo '>Bedrooms High > Low</option>';
echo '<option value="bedrooms_low" ';
if($_SESSION['orderby'] == "bedrooms_low") {
echo 'selected';
}
echo '>Bedrooms Low > High</option>';
echo '</select>';
// echo '<input type="submit" value="Order"/>';
echo '</form>';
echo '</div><!--.order_by_container-->';
while ( $search_query->have_posts() ) {
$search_query->the_post();
$post_id = get_the_ID();
$status_data = get_post_meta( $post_id, '_status', true );
$date_available_day_data = get_post_meta( $post_id, '_date_available_day', true );
$date_available_month_data = get_post_meta( $post_id, '_date_available_month', true );
$date_available_year_data = get_post_meta( $post_id, '_date_available_year', true );
$furnished_unfurnished_data = get_post_meta( $post_id, '_furnished_unfurnished', true );
$property_deposit_data = get_post_meta( $post_id, '_property_deposit', true );
$property_type_data = get_post_meta( $post_id, '_property_type', true );
$location_map_data = get_post_meta( $post_id, '_location_map', true );
$address_line_one_data = get_post_meta( $post_id, '_address_line_one', true );
$address_line_two_data = get_post_meta( $post_id, '_address_line_two', true );
$town_data = get_post_meta( $post_id, '_town', true );
$postcode_one_data = get_post_meta( $post_id, '_postcode_one', true );
$postcode_two_data = get_post_meta( $post_id, '_postcode_two', true );
$tenure_data = get_post_meta( $post_id, '_tenure', true );
$rental_sale_value_data = get_post_meta( $post_id, '_rental_sale_value', true );
$number_bedrooms_data = get_post_meta( $post_id, '_number_bedrooms', true );
$floor_area_data = get_post_meta( $post_id, '_floor_area', true );
$resale_lettings_data = get_post_meta( $post_id, '_resale_lettings', true );
$featured_data = get_post_meta( $post_id, '_featured', true );
echo '<div class="single_property_list">';
if(has_post_thumbnail()) {
echo '<div class="single_property_list_image">';
the_post_thumbnail('property_thumb');
echo '</div><!--.single_property_list_image-->';
}
echo '<div class="single_property_list_details">';
echo '<h2>'.get_the_title().'</h2>';
the_excerpt();
echo '<div class="single_property_list_meta">';
echo '<div class="price">';
echo 'Price: £'.$rental_sale_value_data;
echo '</div><!--.price-->';
echo '<div class="bedrooms">';
echo 'Bedrooms: '.$number_bedrooms_data;
echo '</div><!--.bedrooms-->';
echo '</div><!--.single_property_list_meta-->';
echo '<a href="'.get_permalink().'">';
echo 'View this property';
echo '</a>';
echo '</div><!--.single_property_list_details-->';
echo '</div><!--.single_property_list-->';
}
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $search_query->max_num_pages
) );
} else {
echo '<p>Sorry, no posts matched your criteria</p>';
}
wp_reset_postdata();
echo '</div><!--#page_container-->';
get_footer();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment