1

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?

4
  • I don't think this is possible with WP_Query as is, you'd need to insert custom calculation logic into the SQL query. You might be able to add a new query variable, then add some code to intercept the sql query and add some custom logic to it, I'm not sure how you'd do that though. It might help if you provided the SQL for calculating distance
    – Tom J Nowell
    Commented Jan 22, 2015 at 19:02
  • The plugin recommendation is off-topic here. And the other part of the question is not clear. Do you need help knowing the geolocation of the user or what? Can you explain it again?
    – cybmeta
    Commented Jan 22, 2015 at 19:02
  • How is the lat/lng data saved, as serialized arrays? Maybe you could add more info to your question. ps: I was playing with the Haversine formula yesterday, to support the geo_query parameter in WP_Query on github here. Maybe that could help you?
    – birgire
    Commented Jan 23, 2015 at 2:53
  • Thanks all - sorry the question was a bit vague. I've extended wp_query to support the extra SQL so all sorted.
    – steve
    Commented Jan 23, 2015 at 13:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.