I am building a simple property search website using Wordpress. One of the requirements is for a search tool to search for properties available for sale / to rent based on number of bedrooms etc.
I'm using ACF and storing these attributes in custom fields, and so far have managed to use wp_query to filter the 'property' post type based on search options.
Current code takes the values from the search forms, pops them into session values then builds up the query using simple meta query and tax query arguments e.g.:
if( ! empty( $_SESSION['property-max-price'] ) ) {
$args['meta_query'][] = array(
'key' => 'property_price_numeric',
'value' => $_SESSION['property-max-price'],
'compare' => '<=',
'type' => 'SIGNED'
);
}
if(! empty($_SESSION['property-type'])) {
$translations = array();
$translations['buy'] = 'residential-sales';
$translations['rent'] = 'residential-lettings';
$translations['commercial'] = 'commercial-business';
$args['tax_query'][] = array(
'taxonomy' => 'custom_cat',
'field' => 'slug',
'terms' => array( $translations[$_SESSION['property-type']] ),
'operator' => 'IN'
);
}
I'm having less luck getting a location based search/filter to work. I have experience building store locators using PHP / mysql outside of WP - and understand the sin() based maths to calculate distance between two points, but I'm a bit baffled how best to achieve this in wp_query - or indeed if I need to extend that function.
Also - is there a good WP plugin for geo-coding that provides a function I can use at template level which could be used to geocode the entered postal code/location to lat/lon?
geo_query
parameter inWP_Query
on github here. Maybe that could help you?