Events Manager
[resolved] how to create a search filter for locations (9 posts)

  1. omegacoder
    Posted 3 years ago #

    I have a locations page which lists all event locations. I need to create a search filter for locations on this page below, so users can search for a particular location/venue:


    Can anyone give me some starting points on how to do this? A simple starting point would be to have a search input field and select drop down to search by city

    Thanks in advance!


  2. agelonwl
    Posted 3 years ago #

  3. omegacoder
    Posted 3 years ago #

    thanks, perhaps my wording was incorrect, so I apologise for this

    i need a search input field for locations and not a locations filter i.e. it's a text input where user types in a location name and the system returns any locations that are similar as a list. you know something similar to the events list page where users can type in text to find matching events

  4. omegacoder
    Posted 3 years ago #

    this is to go into the locations list page by the way i.e.


  5. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 3 years ago #

    i see. You'd need to create your own custom search form for this by overriding the templates/templates/locations-list.php template.

    One suggestion for the search form, you can probably re-use the auto-completer field we use on the locations section when creating a new event. Try doing e.g. [event_form] and you'll see the html you need to ouptut there.

  6. omegacoder
    Posted 3 years ago #

    I've checked out the html, it outputs part of the following:

    <input id="location-name" class="ui-autocomplete-input" type="text" value="Shanghai" name="location_name" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true" style="background-color: rgb(255, 255, 255);">

    I'm not asking for a step-guide solution of course, but can you be more specific in what needs to be done to achieve what I need? you know, some pointers with maybe some pesudo code or examples? Thanks thanks

  7. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 3 years ago #

    sorry, you'd need to figure that out yourself.

    you'll see how we we handle it in em-actions.php lin 186 and the autocomplete is added in includes/js/events-manager.js line 533

    we use this, which is already included by WP - http://jqueryui.com/autocomplete/

    There's better ways to handle the ajax call on WP though - http://codex.wordpress.org/AJAX_in_Plugins

  8. omegacoder
    Posted 3 years ago #

    ok, here is the solution I used. Users can search for locations name, address and town:

    in classes/em-locations.php, I added two functions:

    function search_locations($q){
    			global $wpdb;
    			$q = rawurldecode($q);
    			$results = array();
    				$location_cond = (is_user_logged_in() && !current_user_can('read_others_locations')) ? "AND location_owner=".get_current_user_id() : '';
    				if( !is_user_logged_in() && get_option('dbem_events_anonymous_submissions') ){
    					if( !user_can(get_option('dbem_events_anonymous_user'),'read_private_locations') ){
    						$location_cond = " AND location_private=0";
    				}elseif( is_user_logged_in() && !current_user_can('read_private_locations') ){
    				    $location_cond = " AND location_private=0";
    				}elseif( !is_user_logged_in() ){
    					$location_cond = " AND location_private=0";
    				$term = (isset($_REQUEST['term'])) ? '%'.$_REQUEST['term'].'%' : '%'.$q.'%';
    				$sql = $wpdb->prepare("
    						location_id AS <code>id</code>,
    						Concat( location_name )  AS <code>label</code>,
    						location_name AS <code>value</code>,
    						location_address AS <code>address</code>,
    						location_town AS <code>town</code>,
    						location_state AS <code>state</code>,
    						location_region AS <code>region</code>,
    						location_postcode AS <code>postcode</code>,
    						location_country AS <code>country</code>,
    						location_slug AS <code>slug</code>
    					WHERE ( <code>location_name</code> LIKE %s || <code>location_address</code> LIKE %s || <code>location_town</code> LIKE %s) AND location_status=1 $location_cond LIMIT 30
    				", $term, $term, $term);
    				$results_obj = $wpdb->get_results($sql);
    				// convert results from object to array type
    				$results_arr = self::toArray($results_obj);
    				return $results_arr;
    	  // convert results from object to array type
    	  function toArray($obj) {
    		if(is_object($obj)) $obj = (array) $obj;
    		if(is_array($obj)) {
    		  $new = array();
    		  foreach($obj as $key => $val) {
    			$new[$key] = self::toArray($val);
    		else {
    		  $new = $obj;
    		return $new;

    and in templates/templates/locations-list.php, I added the following code:

    <form id="locations-dir-search" method="get" action="<?php echo site_url();?>/events/locations/">
    <input type="hidden" name="action" value="search_locations" />
    search locations <input type="text" id="locations_search" name="q" onblur="if(this.value=='')this.value='venue name, address, city'" onfocus="if(this.value=='venue name, address, city')this.value=''" value="venue name, address, city" />
    <input type="submit" value="search" />
    // if user searchs for location
    if( !empty($_REQUEST['action']) && $_REQUEST['action'] == "search_locations" && (!empty($_REQUEST['q'])) ){
    	//$q = rawurlencode($_REQUEST['q']);
    	$q = $_REQUEST['q'];
    	//print_r('<pre>');print_r( EM_Locations::search_locations($q) ); print_r('</pre>');
    	$locations = EM_Locations::search_locations($q);
    		for($i=0; $i<count($locations); $i++) {
    			echo '<a href="'.site_url().'/events/locations/'.$locations[$i]['slug'].'">'.$locations[$i]['value'] . '</a> ' . $locations[$i]['address'] . '<br/>';
    		echo 'No Locations';
    }else { // display default results of location list
    	echo EM_Locations::output(apply_filters('em_content_locations_args', $args));

    Although, I didn't have time to include the pagination feature that's included in the EM_Locations:output function, and I simply limited the results to 30. Perhaps someone will have the time to post solution here

  9. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 3 years ago #

    thx for sharing!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Events Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.