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

Custom Post Type, Taxonomy, and Geo-Coded Radius Search

I've been struggling with this for the last few days and just keep seem to get it right. Here's the situation:

I have a provider directory (hospitals) in a custom post type. The 2,000+ providers were imported via CSV with location data, for which I then used the geomashup plugin to add coordinates as meta info. So all the posts have the necessary components to getting this done.

Now what I need to do is build a search for on the frontpage that lets the user enter a zip code or city (text box), select a radius (in miles, from a dropdown list), and (optionality) a specialty (which is stored in a custom taxonomy).

I've messed with a loop based on the query info in the geomashup docs, but to no avail. I've been plugging in specific lat/long coordinates which should return results, yet nothing shows up.

Here's the query code I've been using:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $rq = new WP_Query( array( 'posts_per_page' => 9999, 'paged' => $paged, 'geo_mashup_query' => array( 'near_lat' => 26.055, 'near_lng' => -80.153, 'radius_mi' => 50, ), ) ); while ($rq->have_posts()) : $rq->the_post(); ?>

Ultimately, I need help figuring out how to configure the search form on the homepage, pass the variables to the search results page, and get results to show up. There are various "find nearby" plugins and answers here related to radius searches, but there are simply too many of these items and they needed to belong in a CPT w/ custom taxonomy and not be plugin based.

A solution to this is urgent as the client has now sprung end of day tomorrow as the due date. :S

Answers (3)


Arnav Joy answers:

are you using any plugin for geomashup search?


Bob answers:

Not sure.

But I think you are missing post type in your query.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 9999,
'post_type' => 'your_post_type',
'paged' => $paged,
'geo_mashup_query' => array(
'near_lat' => 26.055,
'near_lng' => -80.153,
'radius_mi' => 50
$rq = new WP_Query( $args );
while ($rq->have_posts()) : $rq->the_post(); ?>


Dylan Kuhn answers:

If you haven't yet, try using Geo Mashup's search widget. It will let you configure all the parameters you're looking for except post type and taxonomy. The simplest way to add that is to customize the widget search form following the instructions in geo-mashup/default-templates/search-form.php. You can just add some hidden fields for post type:

<input name="object_name" type="hidden" value="post" />
<input name="map_post_type" type="hidden" value="locations" />

See if you can get that working first.

For the custom taxonomy you'll add your own dropdown to the form, and then use the geo_mashup_search_query_args filter to add a tax_query parameter based on the selected dropdown value. You can try some filter code like this in your functions.php or equivalent:

add_filter( 'geo_mashup_search_query_args', 'prefix_add_custom_tax_query' );

function prefix_add_custom_tax_query( $args ) {
if ( isset( $_POST['taxonomy_dropdown'] ) ) {
$args['tax_query'] = array( array(
'taxonomy' => 'custom_taxonomy',
'terms' => $_POST['taxonomy_dropdown'],
) );
return $args;

The code to generate the dropdown in your search form will be something like this:

<?php $terms = get_terms( 'custom_taxonomy' ); ?>
<select name="custom_taxonomy">
<?php foreach( $terms as $term ) : ?>
<option value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php endforeach; ?>

antix23 comments:

Hey Dylan,

The form seems to be fine now, but the results page yields nothing. Seems like the query vars don't get passed to the output loop, so it just returns blank. All I need was set a page "directory results" as the search results page within the widget, and dropped the geomashup search-results.php loop in a custom page template for that (search results) page.

<div id="geo-mashup-search-results">

<h2><?php _e( 'Search results near', 'GeoMashup' ); ?> "<?php echo $search_text; ?>"</h2>

<?php if ( $geo_mashup_search->have_posts() ) : ?>

<?php echo GeoMashup::map( array(
'name' => 'search-results-map',
'search_text' => $search_text,
'object_ids' => $geo_mashup_search->get_the_ID_list(),
'center_lat' => $near_location['lat'],
'center_lng' => $near_location['lng'],
'search_lat' => $near_location['lat'],
'search_lng' => $near_location['lng'],
'object_name'=> $object_name,
'zoom' => $approximate_zoom + 1 // Adjust to taste
) ); ?>

<?php if ($object_name == 'post'):?>

<?php while ( $geo_mashup_search->have_posts() ) : $geo_mashup_search->the_post(); ?>
<div class="search-result">
<h3><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
<?php _e( 'Distance', 'GeoMashup' ); ?>:
<?php $geo_mashup_search->the_distance(); ?>
<?php endwhile; ?>
<?php elseif ($object_name == 'user'):?>

<?php while ( $geo_mashup_search->have_posts() ) : $user=$geo_mashup_search->get_userdata(); ?>
<div class="search-result">
<h3><?php echo $user->first_name.' '.$user->last_name;?> aka <?php echo $user->user_nicename?></h3>
<?php _e( 'Distance', 'GeoMashup' ); ?>:
<?php $geo_mashup_search->the_distance(); ?>
<?php endwhile; ?>
<?php endif;?>
<?php else : ?>

<p><?php _e( 'No results found.', 'GeoMashup' ); ?></p>

<?php endif; ?>

Dylan Kuhn comments:

Normally the widget filters the_content to add the search results, so I'm not sure if the search is getting instantiated in your setup. Can you send the output of a couple of var_dump calls?

var_dump( $_POST );
var_dump( $geo_mashup_search );

Dylan Kuhn comments:

I guess we won't get that. The easiest thing would be to follow the directions in the search results template for customizing it so the search is run automatically by the widget and results are added to the selected page by the filter on the_content. If that route won't work for some reason, you could try instantiating the search object yourself in the template:

$geo_mashup_search = new GeoMashupSearch( $_POST );

antix23 comments:

Hey Dylan,

Once i got everything sorted out, I was able to customize the template. The frontend (customized) widget search is working along with the results page, but with two minor issues. The taxonomy dropdown, while populating, doesn't seem to be affecting the actual search results (even though I have the filter you previously mentioned in the functions file). I also need to paginate search results since there are so many items in this directory.