[Plugin: Events Manager] Search Events by Location
-
Maybe I am just missing it, but is there a way to maybe enter an address and show locations near that address?
-
we don’t have this feature
I didn’t think so, I ended up writing my own search that works with the em_locations table. Thank you for the reply. =)
sharing might be a good idea to help the community 🙂
I agree, and I usually do share. Ok, first you need to find a free zipcode database online, I can’t remember the one I used but you can search on Google. Then put it into your database in a table called wp_hc_zipcodes with at least the zip int(5), lat float(10,6), lng float(10,6) fields.
Now the below probably could be optimized, but I just threw it together for functionality. I used it as a shortcode [locationsearch]
add_shortcode('locationsearch','em_location_search'); function em_location_search() { global $wpdb; $table_name = $wpdb->prefix . "hc_zipcodes"; $output = '<div class="loc-search-form">Search locations.<br /><form method="post" action="">'; $output .= '<label for="mbn_zipcode">Zip Code:</label> <input type="text" name="mbn_zipcode" id="mbn_zipcode" maxlength="5" size="7" /> '; $output .= '<label for="mbn_distance">within </label><select name="mbn_distance" id="mbn_distance" tabindex="2"> <option value="5">5</option> <option value="10">10</option> <option value="25">25</option> <option value="50">50</option> <option value="100">100</option> </select> miles '; $output .= '<label for="state"><strong>OR</strong> by State:</label> <select name="state" id="state" tabindex="3"><option></option>'; $states = mysql_query('SELECT DISTINCT location_state FROM ' . $wpdb->prefix . 'em_locations'); while ($state = mysql_fetch_array($states)) { $output .= '<option value="' . $state[0] . '">' . $state[0] . '</option>'; } $output .= '</select>'; $output .= '<input type="submit" id="submit" name="submit" value="Search" /> '; $output .= '</form></div>'; if ($_POST['submit'] && ($_POST['mbn_zipcode'] || $_POST['state'])) { if ($_POST['state']) { $stores = mbn_get_stores_by_state( $_POST['state'] ); $stores_total = count( $stores ); if( $stores_total > 0 ) { $output .= '<div class="loc-search-result"><h3>Search Results</h3>'; $output .= '<ol>'; foreach( $stores as $store) { $output .= '<li> <a href="'.get_bloginfo('wpurl').'/locations/ ' . $store[0] . '"> ' . $store[1] . '</a> Distance (' . $store[6] . ' miles)<br /> ' . $store[2] . ', ' . $store[3] . ', ' . $store[4] . ', ' . $store[5] . ' </li>'; } $output .= '</ol></div>'; } else { $output .= '<p>No results within the area searched.</p>'; } } else { if( !is_numeric( $_POST['mbn_zipcode'] ) ) { $the_zip = '0'; } else { $the_zip = $_POST['mbn_zipcode']; } $the_distance = intval( $_POST['mbn_distance'] ); $stores = mbn_get_stores_by_location( $the_zip, $the_distance ); $stores_total = count( $stores ); if( $stores_total > 0 ) { $output .= '<div class="loc-search-result"><h3>Search Results</h3>'; $output .= '<ol>'; foreach( $stores as $store) { $output .= '<li> <a href="'.get_bloginfo('wpurl').'/locations/ ' . $store[0] . '"> ' . $store[1] . '</a> Distance (' . $store[6] . ' miles)<br /> ' . $store[2] . ', ' . $store[3] . ', ' . $store[4] . ', ' . $store[5] . ' </li>'; } $output .= '</ol></div>'; } else { $output .= '<p>No results within the area searched.</p>'; } } } return $output; } function mbn_get_stores_by_state( $state ) { global $wpdb; $sql = ' SELECT location_id, location_slug, location_name, location_address, location_town, location_state, location_postcode FROM ' . $wpdb->prefix . 'em_locations WHERE location_state = "' . $state . '" ORDER BY location_name '; $nearby_zips = mysql_query( $sql ) or die(mysql_error()); // we need to store the zips in order to build the Pods query $target_zips = array(); WHILE ($row = mysql_fetch_array($nearby_zips)) { $target_zips[$row['location_id']][] = $row['location_slug']; $target_zips[$row['location_id']][] = $row['location_name']; $target_zips[$row['location_id']][] = $row['location_address']; $target_zips[$row['location_id']][] = $row['location_town']; $target_zips[$row['location_id']][] = $row['location_state']; $target_zips[$row['location_id']][] = $row['location_postcode']; $target_zips[$row['location_id']][] = round($row['distance'], 2); } return $target_zips; } function mbn_get_stores_by_location( $zip, $radius ) { global $wpdb; $radius = intval( $radius ); // we first need to get the source coordinates $sql = mysql_query('SELECT <code>lat</code>, <code>lng</code> FROM ' . $wpdb->prefix . 'hc_zipcodes WHERE zipcode = "' . $zip . '" LIMIT 1'); $coords = mysql_fetch_row( $sql ); // now we'll get the other ZIP codes within the radius, ordered by distance $sql = ' SELECT location_id, location_slug, location_name, location_address, location_town, location_state, location_postcode, ( 3959 * acos( cos( radians( ' . $coords[0] . ' ) ) * cos( radians( location_latitude ) ) * cos( radians( location_longitude ) - radians( ' . $coords[1] . ' ) ) + sin( radians( ' . $coords[0] . ' ) ) * sin( radians( location_latitude ) ) ) ) AS distance FROM ' . $wpdb->prefix . 'em_locations HAVING distance <= ' . $radius . ' OR distance IS NULL ORDER BY distance LIMIT 0 , 30;'; $nearby_zips = mysql_query( $sql ) or die(mysql_error()); // we need to store the zips in order to build the Pods query $target_zips = array(); WHILE ($row = mysql_fetch_array($nearby_zips)) { $target_zips[$row['location_id']][] = $row['location_slug']; $target_zips[$row['location_id']][] = $row['location_name']; $target_zips[$row['location_id']][] = $row['location_address']; $target_zips[$row['location_id']][] = $row['location_town']; $target_zips[$row['location_id']][] = $row['location_state']; $target_zips[$row['location_id']][] = $row['location_postcode']; $target_zips[$row['location_id']][] = round($row['distance'], 2); } return $target_zips; }
thanks for sharing.
Thanks for sharing! Bookmarked this for possible later use.
- The topic ‘[Plugin: Events Manager] Search Events by Location’ is closed to new replies.