It is possible for the users of WP Event Manager to add new filters to the event search form. In this guide we will show you how you can do it.
Creating a custom event search filter
WP Event Manager’s [Events] shortcode shows a search filter on the top. if you want to add a custom filter in the top section, you can add it via hook or overriding template files.
You can easily add new filters to the event search form by adding a field and modifying the search queries using the filters.
Here are the steps you need to follow :
- Open your child theme.
- Add the code into the funtions.php file.
Here is an example field:
/*** Adding via filter or you can directly add in a template file*/add_action('event_manager_event_filters_search_events_end','filter_by_country_field');functionfilter_by_country_field() { ?><divclass="wpem-row"><divclass="wpem-col"><divclass="wpem-form-group"><divclass="search_event_types"><labelfor="search_event_types"class="wpem-form-label"><?php _e('Country','event_manager'); ?></label><select name="filter_by_country"class="event-manager-filter"><option value=""><?php _e('Select country','event_manager'); ?></option><option value="de"><?php _e('Germany','event_manager'); ?></option><option value="in"><?php _e('India','event_manager'); ?></option><option value="us"><?php _e('USA','event_manager'); ?></option></select></div></div></div></div>- <?php
- }
- /**
* This code gets your posted field and modifies the event search query*/add_filter('event_manager_get_listings','filter_by_country_field_query_args', 10, 2 );functionfilter_by_country_field_query_args($query_args,$args) {if( isset($_POST['form_data'] ) ) {parse_str($_POST['form_data'],$form_data);// If this is set, we are filtering by countryif( !empty($form_data['filter_by_country'] ) ) {$event_country= sanitize_text_field($form_data['filter_by_country'] );$query_args['meta_query'][] =array('key'=>'_event_country','value'=>$event_country,);}}return$query_args;}
- Save Changes.
- Create a new field in the field editor(WP-Admin >> Event Manager >> Field Editor).
- The field created should be with the meta key value( _event_country).
- The placeholder should contain the same value, as provided in the code( de | in | us).
- Save changes.
- Link your Events with the country and finally you can filter them on the frontend.