Add custom field in Search filter
Creating a custom event search filter
WP Event Manager’s [Events] shortcode will show a search filter on top. if you want to add a custom filter in the top section you can add it via hook or overriding via template files.
It is possible to add new filters to the search form by adding a field and then modifying the search queries using filters.
- Open your child theme,
- Add the code into the funtions.php file.
Here is an example field:
<?php /** * 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' ); function filter_by_country_field() { ?> <div class="wpem-row"> <div class="wpem-col"> <div class="wpem-form-group"> <div class="search_event_types"> <label for="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 ); function filter_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 country if ( ! 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.