Add custom field in Search filter

Creating a custom event search filter

The [Events] shortcode will show search filter on top. if you want to add custom filter in top senction 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.

Here is an example field:

 

<?php 
/**
 * Adding via filter or you can directly add in template file
 */
add_action( 'event_manager_event_filters_search_events_end', 'filter_by_country_field' );
function filter_by_country_field() {
    ?>
    <div class="search_event_types">
        <label for="search_event_types"><?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>
    <?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;
}