Custom search - filter through custom post types
<?php
/*
Template Name: Property Listings
*
* This is your custom page template. You can create as many of these as you need.
* Simply name is "page-whatever.php" and in add the "Template Name" title at the
* top, the same way it is here.
*
* When you create your page, you can just select the template and viola, you have
* a custom page template to call your very own. Your mother would be so proud.
*
* For more info: http://codex.wordpress.org/Page_Templates
*/
// error_reporting(E_ALL);
// ini_set('display_errors', '1');
function debugCode($variable){
echo '<pre>';
print_r($variable);
echo '</pre>';
}
?>
<?php get_header(); ?>
<?php include ('partials/epic_property_search.php'); ?>
<section id="content" class="container">
<div class="row">
<div class="col-sm-12">
<main class="content-body" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">
<?php
//Grab whatever values from the following select[names]
$data[0] = $_GET['property_type'];
$data[1] = $_GET['location'];
$data[2] = $_GET['rooms'];
$data[3] = $_GET['min_value'];
$data[4] = $_GET['max_value'];
if(empty($data[2])) {
$data[2] = array(
1, 2, 3, 4, 5
);
}
// debugCode($data);
//Build an array for towns so we can dynamically populate our select list options
$townList = array();
$metakey = 'town_index';
$towns = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
if ($towns) {
foreach ($towns as $town) {
array_push($townList, $town);
}
}
//debugCode($townList);
$propertyType = array();
/*
We want to run the same code but for different forms so we create a variable to differentiate
what form submit is being used.
Create a hidden input above submit with value="buy"
Create a hidden input above submit with value="rent"
Create a hidden input above submit with value="shared"
So now the variable $propertyType will equal to what post type we want it to return
*/
if($_GET['propertyType'] == "buy"){
$propertyType = array('property_to_buy');
} elseif($_GET['propertyType'] == "rent") {
$propertyType = array('property_to_rent');
} elseif($_GET['propertyType'] == "shared") {
$propertyType = array('shared_ownership');
} else {
$propertyType = array( 'property_to_buy', 'property_to_rent', 'shared_ownership' );
}
$propertyValue = 'property_value';
if($_GET['propertyType'] == "rent"){
$propertyValue = 'rent_value';
}
// Look at the URL and see if it has 'propertyType'
if( !empty($_GET['propertyType']) ) {
//if <select name="property_type"> and <select name="location"> values chosen are "Any"
if( $data[0] == 'Any' && $data[1] == 'Any' ) {
echo 1;
//WP pagination enabled
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Build my array with this list of arguments
$args = array(
'post_type' => $propertyType[0], //Because it's an array, it needs the index number
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type_of_property',
'value' => array('detached', 'semidetached', 'midterrace', 'endterrace', 'apartment', 'bungalow'),
'compare' => 'IN' //Compare all posts within above
),
array(
'key' => $propertyValue,
'value' => array($data[3], $data[4]),
'type' => 'DECIMAL',
'compare' => 'BETWEEN' //Compare min and max property value
),
array(
'key' => 'number_of_bedrooms',
'value' => $data[2],
'compare' => 'IN'
),
array(
'key' => 'town_index',
'value' => $townList,
'compare' => 'IN'
),
)
);
}
elseif( $data[0] != 'Any' && $data[1] == 'Any') {
// echo 2;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => $propertyType[0],
'paged'=>$paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type_of_property',
'value' => $data[0],
'compare' => 'IN'
),
array(
'key' => $propertyValue,
'value' => array($data[3], $data[4]),
'type' => 'DECIMAL',
'compare' => 'BETWEEN'
),
array(
'key' => 'number_of_bedrooms',
'value' => $data[2],
'compare' => 'IN'
),
array(
'key' => 'town_index',
'value' => $townList,
'compare' => 'IN'
),
)
);
}
elseif( $data[0] == 'Any' && $data[1] != 'Any' ) {
// echo 2.5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => $propertyType[0],
'paged'=>$paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type_of_property',
'value' => array('detached', 'semidetached', 'midterrace', 'endterrace', 'apartment', 'bungalow'),
'compare' => 'IN'
),
array(
'key' => $propertyValue,
'value' => array($data[3], $data[4]),
'type' => 'DECIMAL',
'compare' => 'BETWEEN'
),
array(
'key' => 'number_of_bedrooms',
'value' => $data[2],
'compare' => 'IN'
),
array(
'key' => 'town_index',
'value' => $data[1],
'compare' => 'IN'
),
)
);
}
else {
// echo 3;
//If options have been selected and not any
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => $propertyType[0],
'paged'=>$paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type_of_property',
'value' => $data[0],
),
array(
'key' => $propertyValue,
'value' => array($data[3], $data[4]),
'type' => 'DECIMAL',
'compare' => 'BETWEEN'
),
array(
'key' => 'number_of_bedrooms',
'value' => $data[2],
'compare' => 'IN'
),
array(
'key' => 'town_index',
'value' => $data[1],
'compare' => 'IN'
),
)
);
}
$loop = new WP_Query( $args );
query_posts($args);
} else {
// echo 4;
//Finally display all posts in all custom post type categories and display them at first load
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'post_type' => array( 'property_to_buy', 'property_to_rent', 'shared_ownership' ),
'orderby' => 'date',
'order' => 'desc',
'paged' => $paged
);
$loop = new WP_Query( $args );
query_posts($args);
}
?>
<?php
$type_of_property = get_post_meta( $post->ID, 'type_of_property', true );
if ( $loop->have_posts() ) : ?>
<?php
$propertyType = str_replace(
['buy', 'shared', 'rent'],
['Buy Properties', 'Shared Ownership', 'Rent Properties'], $_GET['propertyType']
);
if( !empty($_GET['propertyType']) ) {
echo '<h1 id="search-results">Search Results - ' . $propertyType . '</h1>';
}
else {
echo '<h1>Places to Live</h1>';
}
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'property-listing' ); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<a class="property-ft-img" style="background-image: url('<?php echo $image[0]; ?>')" href="<?php the_permalink(); ?>">
<span class="type-listing">
<?php $post_type = get_post_type_object( get_post_type($post) ); echo $post_type->label; ?>
</span>
</a>
<div class="property-details">
<div class="price">
<?php
$price = get_field('property_value');
$rentPrice = get_field('rent_value');
if($price) : ?>
<?php echo '£' . $propertyValuePrice = number_format( floatval( get_field('property_value') ), 0, '.', ',' ); ?>
<?php elseif($rentPrice) : ?>
<?php echo '£' . $propertyValuePrice = number_format( floatval( get_field('rent_value') ), 0, '.', ',' ) . ' pcm'; ?>
<?php elseif($price == 0 || $rentPrice == 0) : ?>
<?php echo 'TBC'; ?>
<?php endif; ?>
</div>
<div class="short-desc">
<?php the_field('number_of_bedrooms'); ?> Bedroom <?php the_field('type_of_property'); ?>
</div>
<div class="address"><?php the_title(); ?></div>
<div class="property-excerpt">
<?php the_field('property_excerpt'); ?>
</div>
<div class="property-links">
<?php $sharedEnquiry = get_field('enquiry'); if(empty($sharedEnquiry)): ?>
<a href="tel:+4401634921802" class="enquiry">
<span><strong>Enquiries</strong> +44 (0) 163 492 1802</span>
<i class="fa fa-phone"></i>
</a>
<?php else: ?>
<a href="tel:+440<?php the_field('enquiry'); ?>" class="enquiry">
<span><strong>Enquiries</strong> +44 (0) <?php the_field('enquiry'); ?></span>
<i class="fa fa-phone"></i>
</a>
<?php endif; ?>
<a href="<?php the_permalink(); ?>" class="view-property">
<span>View property</span>
<i class="fa fa-angle-right"></i>
</a>
</div>
</div>
</article>
<?php endwhile; wp_reset_postdata(); ?>
<?php wp_pagenavi(array('query' => $loop)); ?>
<?php else : ?>
<h1>No search results found!</h1>
<?php endif; wp_reset_query();?>
</main>
</div>
<!-- /row -->
</div>
</section>
<?php get_footer(); ?>
<?php
function max_meta_value(){
global $wpdb;
//$query = "SELECT max(meta_value) FROM wp_postmeta WHERE meta_key='property_value'";
$query = "SELECT meta_value FROM wp_postmeta WHERE meta_key = 'property_value' ORDER BY CAST(`meta_value` AS DECIMAL) DESC LIMIT 1";
$the_max = $wpdb->get_var($query);
return $the_max;
};
?>
<?php
function max_meta_rent_value(){
global $wpdb;
//$query = "SELECT max(meta_value) FROM wp_postmeta WHERE meta_key='property_value'";
$query = "SELECT meta_value FROM wp_postmeta WHERE meta_key = 'rent_value' ORDER BY CAST(`meta_value` AS DECIMAL) DESC LIMIT 1";
$the_max = $wpdb->get_var($query);
return $the_max;
};
?>
<?php $url = 'http://' . $_SERVER['HTTP_HOST']; ?>
<?php
$currentURL = $_SERVER['REQUEST_URI'];
if($currentURL == '/property_to_rent/' || $_GET['propertyType'] == "rent"){
$rentTab = 'active';
} elseif($currentURL == '/shared_ownership/' || $_GET['propertyType'] == "shared"){
$sharedTab = 'active';
} else {
$buyTab = 'active';
}
?>
<section class="property-search-bar">
<div class="container property-option-tabs">
<ul class="nav nav-tabs nav-justified" role="tablist">
<li role="presentation" class="<?php echo $buyTab; ?>">
<a href="#buy" aria-controls="home" role="tab" data-toggle="tab">Buy</a>
</li>
<li role="presentation" class="<?php echo $rentTab; ?>">
<a href="#rent" aria-controls="profile" role="tab" data-toggle="tab">Rent</a>
</li>
<li role="presentation" class="<?php echo $sharedTab; ?>">
<a href="#shared-ownership" aria-controls="profile" role="tab" data-toggle="tab">Shared Ownership</a>
</li>
</ul>
<!-- / property-option-tabs -->
</div>
<div class="tab-content property-option-content">
<div role="tabpanel" class="tab-pane <?php echo $buyTab; ?>" id="buy">
<div class="container">
<form class="property-search" action="<?php echo $url; ?>/places-to-live/#search-results" method="get">
<div class="row">
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Property Type</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select class="form-control" name="property_type">
<option value="Any">Any</option>
<option value="detached">Detached House</option>
<option value="semidetached">Semi-Detached House</option>
<option value="midterrace">Mid-Terrace House</option>
<option value="endterrace">End of Terrace House</option>
<option value="apartment">Apartment</option>
<option value="bungalow">Bungalow</option>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Location</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select class="form-control" name="location">
<option value="Any">Any</option>
<?php
$metakey = 'town_index';
$towns = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
if ($towns) {
foreach ($towns as $town) {
echo "<option value=\"" . $town . "\">" . $town . "</option>";
}
}
?>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<!--/first row-->
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Rooms</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select name="rooms[]" class="room-selection" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Price</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<div class="row">
<div class="col-xs-6">
<select class="form-control" name="min_value">
<option value="0">Min (£) Any</option>
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
<option value="400000">£500,000</option>
<option value="400000">£600,000</option>
<option value="400000">£700,000</option>
<option value="400000">£800,000</option>
<option value="400000">£900,000</option>
</select>
</div>
<div class="col-xs-6">
<select class="form-control" name="max_value">
<option value="<?php echo max_meta_value(); ?>">Max (£) Any</option>
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
<option value="400000">£500,000</option>
<option value="400000">£600,000</option>
<option value="400000">£700,000</option>
<option value="400000">£800,000</option>
<option value="400000">£900,000</option>
</select>
</div>
</div>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<!--/second row-->
</div>
<div class="row">
<div class="col-xs-6">
<button class="btn-reset" type="reset">Reset filters</button>
</div>
<div class="col-xs-6">
<input type="hidden" name="propertyType" value="buy">
<button name="submit" class="btn-submit" type="submit">Find my new home</button>
</div>
</div>
<!--/form-->
</form>
<!--/container-->
</div>
<!--/Buy tabbed content-->
</div>
<div role="tabpanel" class="tab-pane <?php echo $rentTab; ?>" id="rent">
<div class="container">
<form class="property-search" method="get" action="<?php echo $url; ?>/places-to-live/#search-results">
<div class="row">
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Property Type</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select class="form-control" name="property_type">
<option value="Any">Any</option>
<option value="detached">Detached House</option>
<option value="semidetached">Semi-Detached House</option>
<option value="midterrace">Mid-Terrace House</option>
<option value="endterrace">End of Terrace House</option>
<option value="apartment">Apartment</option>
<option value="bungalow">Bungalow</option>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Location</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select class="form-control" name="location">
<option value="Any">Any</option>
<?php
$metakey = 'town_index';
$towns = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
if ($towns) {
foreach ($towns as $town) {
echo "<option value=\"" . $town . "\">" . $town . "</option>";
}
}
?>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<!--/first row-->
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Rooms</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select name="rooms[]" multiple class="room-selection">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Price</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<div class="row">
<div class="col-xs-6">
<select class="form-control" name="min_value">
<option value="0">Min (£/PCM) Any</option>
<option value="100">£100</option>
<option value="200">£200</option>
<option value="300">£300</option>
<option value="400">£400</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option>
<option value="1000">£1000</option>
<option value="1100">£1100</option>
<option value="1200">£1200</option>
<option value="1300">£1300</option>
<option value="1400">£1400</option>
<option value="1500">£1500</option>
</select>
</div>
<div class="col-xs-6">
<select class="form-control" name="max_value">
<option value="<?php echo max_meta_rent_value(); ?>">Max (£/PCM) Any</option>
<option value="100">£100</option>
<option value="200">£200</option>
<option value="300">£300</option>
<option value="400">£400</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option>
<option value="1000">£1000</option>
<option value="1100">£1100</option>
<option value="1200">£1200</option>
<option value="1300">£1300</option>
<option value="1400">£1400</option>
<option value="1500">£1500</option>
</select>
</div>
</div>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<!--/second row-->
</div>
<div class="row">
<div class="col-xs-6">
<button class="btn-reset" type="reset">Reset filters</button>
</div>
<div class="col-xs-6">
<input type="hidden" name="propertyType" value="rent">
<button name="submit" class="btn-submit" type="submit">Find my new home</button>
</div>
</div>
<!--/form-->
</form>
<!--/container-->
</div>
<!--/Buy tabbed content-->
</div>
<div role="tabpanel" class="tab-pane <?php echo $sharedTab; ?>" id="shared-ownership">
<div class="container">
<form class="property-search" method="get" action="<?php echo $url; ?>/places-to-live/#search-results">
<div class="row">
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Property Type</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select class="form-control" name="property_type">
<option value="Any">Any</option>
<option value="detached">Detached House</option>
<option value="semidetached">Semi-Detached House</option>
<option value="midterrace">Mid-Terrace House</option>
<option value="endterrace">End of Terrace House</option>
<option value="apartment">Apartment</option>
<option value="bungalow">Bungalow</option>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Location</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select class="form-control" name="location">
<option value="Any">Any</option>
<?php
$metakey = 'town_index';
$towns = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
if ($towns) {
foreach ($towns as $town) {
echo "<option value=\"" . $town . "\">" . $town . "</option>";
}
}
?>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<!--/first row-->
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Rooms</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<select name="rooms[]" multiple class="room-selection">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<div class="col-md-6">
<div class="form-group clearfix">
<label class="col-xxs-12 col-xs-4 col-md-3">Price</label>
<div class="col-xxs-12 col-xs-8 col-md-9">
<div class="row">
<div class="col-xs-6">
<select class="form-control" name="min_value">
<option value="0">Min (£) Any</option>
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
<option value="400000">£500,000</option>
<option value="400000">£600,000</option>
<option value="400000">£700,000</option>
<option value="400000">£800,000</option>
<option value="400000">£900,000</option>
</select>
</div>
<div class="col-xs-6">
<select class="form-control" name="max_value">
<option value="<?php echo max_meta_value(); ?>">Max (£) Any</option>
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
<option value="400000">£500,000</option>
<option value="400000">£600,000</option>
<option value="400000">£700,000</option>
<option value="400000">£800,000</option>
<option value="400000">£900,000</option>
</select>
</div>
</div>
</div>
<!-- /form-group-->
</div>
<!--//col-md-6 -->
</div>
<!--/second row-->
</div>
<div class="row">
<div class="col-xs-6">
<button class="btn-reset" type="reset">Reset filters</button>
</div>
<div class="col-xs-6">
<input type="hidden" name="propertyType" value="shared">
<button name="submit" class="btn-submit" type="submit">Find my new home</button>
</div>
</div>
<!--/form-->
</form>
<!--/container-->
</div>
<!--/Buy tabbed content-->
</div>
<!--/end of tabbed content-->
</div>
</section>