Show locations based on a keyword search


In this tutorial, you’ll see how you can display locations on the map based on a keyword. For example, you can display the map on your custom search page which contains a text field for users to search by keyword.

For this, we will make use of the predefined variable $_GET.

Edit the file “functions.php” of your theme/child theme and copy/paste the code below:

function cspm_keyword_search($query_args){

   if(isset($_GET['keyword'])){
      $query_args = array(         
         's' => sanitize_text_field($_GET['keyword']),
      );
   }

   return $query_args;

}
add_filter('cs_progress_map_main_query', 'cspm_keyword_search', 10, 1);

The above code should work with any page in your theme. The URL should be something like www.website.com/?keyword=london.

If you want to display the map in the default WP search page of your theme/child theme, use this code instead:

function cspm_keyword_search($query_args){

   if(isset($_GET['s'])){
      $query_args = array(
         's' => get_query_var('s'),
      );
   }

   return $query_args;

}
add_filter('cs_progress_map_main_query', 'cspm_keyword_search', 10, 1);

The above code should work with default search page of your theme. The URL should be something like www.website.com/?s=london.


In the same context