Ask your WordPress questions! Pay money and get answers fast! (more info)

Zip Code Search - WP Geo Mashup WordPress

  • SOLVED

Hi, I need to use

https://wordpress.org/plugins/geo-mashup/

For a zip code search (US Only) integrated into my main search.

Flow is like this:

1. User enters their zipcode into a search text input box
2. use pre_get_posts to build a query to output the results

https://github.com/cyberhobo/wordpress-geo-mashup/wiki/PHP-API#wp_query-integration

Documentation above is for integrating into WP_Query, figured that would be useful. Seems very straight forward for the right person!

Answers (3)

2017-10-31

Pau answers:

Try adding this in functions.php


add_action( 'pre_get_posts', 'geo_postal_search' );
function geo_postal_search( $query ){
if( is_search() && !is_admin() && $query->is_main_query() ){
if( preg_match( "/^([0-9]{5})(-[0-9]{4})?$/i", get_search_query() ) ){
$query->set( 's', '' );
$query->set( 'meta_key', 'geo_mashup_postal_code' );
$query->set( 'meta_value', get_search_query() );
}
}
}


User179251 comments:

Hmm Im not sure this makes sense. I believe you have to use something like this:


$zip_query = new WP_Query( array(
'posts_per_page' => -1,
'geo_mashup_query' => array(
'postal_code' => '96161',
),
) );


User179251 comments:

Its also possible that a reverse geocoding needs to happen, because the search should also accept a radius, maybe this was improperly posted. I wasn't aware this query may not accept additional options like the radius search:

$radius_query = new WP_Query( array(
'posts_per_page' => -1,
'geo_mashup_query' => array(
'near_lat' => 39,
'near_lng' => -120,
'radius_km' => 50,
),
) );


User179251 comments:

Before I was doing something like this, but it didnt seem to be working:


add_action( 'pre_get_posts', 'advanced_search_query' );
function advanced_search_query( $query ) {

if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && ! is_admin() && $query->is_search && $query->is_main_query() ) {


$address = get_search_query();
$coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true');
$coordinates = json_decode($coordinates);

$lat = $coordinates->results[0]->geometry->location->lat;
$lon = $coordinates->results[0]->geometry->location->lng;

$args = array(
'geo_mashup_query' => array(
'near_lat' => $lat,
'near_lng' => $lon,
'radius_km' => 50,
),
);

global $query;
$query = new WP_Query( $args );
}
}


Pau comments:

if going to use their query API then try this:

add_action( 'pre_get_posts', 'geo_postal_search' );
function geo_postal_search( $query ){
if( is_search() && !is_admin() && $query->is_main_query() ){
if( preg_match( "/^([0-9]{5})(-[0-9]{4})?$/i", get_search_query() ) ){
$query->set( 's', '' );
$query->query( array(
'posts_per_page' => -1,
'geo_mashup_query' => array(
'postal_code' => get_search_query()
)
) );
}

}
}


User179251 comments:

Yeah, this one doesnt even get me to the search results page.


Pau comments:

Did you try my code alone or you customize it?


User179251 comments:

Tried your code alone.


Pau comments:

if it doesn't get you into search results page then possible it has errors, the code I gave doesn't display error on my end so how about I check on it directly. add me on skype: akosipau

2017-11-01

Arnav Joy answers:

are you still looking for help ?


User179251 comments:

Yes, I am.


Arnav Joy comments:

is it possible you can show me your site ?
please add me on skype : arnav.joy


User179251 comments:

Sent you a contact request

2017-11-01

Reigel Gallarde answers:

still need help?