Add custom field in Search filter

Add custom field in Search filter

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 :

  1. Open your child theme.
  2. Add the code into the funtions.php file.

    Here is an example field:

  1. /**
  2.  * Adding via filter or you can directly add in a template file
  3.  */
  4.    add_action( 'event_manager_event_filters_search_events_end', 'filter_by_country_field' );
  5.    function filter_by_country_field() { ?>
  6. <div class="wpem-row">
  7.    <div class="wpem-col">
  8.         <div class="wpem-form-group">
  9.                 <div class="search_event_types">
  10.                 <label for="search_event_types" class="wpem-form-label"><?php _e( 'Country', 'event_manager' ); ?></label>
  11.                 <select name="filter_by_country" class="event-manager-filter">
  12.                     <option value=""><?php _e( 'Select country', 'event_manager' ); ?></option>
  13.                     <option value="de"><?php _e( 'Germany', 'event_manager' ); ?></option>
  14.                     <option value="in"><?php _e( 'India', 'event_manager' ); ?></option>
  15.                     <option value="us"><?php _e( 'USA', 'event_manager' ); ?></option>
  16.                 </select>
  17.             </div>
  18.         </div>
  19.     </div>
  20. </div>
  21. <?php
  22. }
  23. /**
  24.  * This code gets your posted field and modifies the event search query
  25.  */
  26. add_filter( 'event_manager_get_listings', 'filter_by_country_field_query_args', 10, 2 );
  27. function filter_by_country_field_query_args( $query_args, $args ) {
  28.     if ( isset( $_POST['form_data'] ) ) {
  29.         parse_str( $_POST['form_data'], $form_data );
  30.         // If this is set, we are filtering by country
  31.         if ( ! empty( $form_data['filter_by_country'] ) ) {
  32.             $event_country = sanitize_text_field( $form_data['filter_by_country'] );
  33.             $query_args['meta_query'][] = array(
  34.                         'key'     => '_event_country',
  35.                         'value'   => $event_country,
  36.                         );
  37.         }
  38.     }
  39.     return $query_args;
  40. }
  1. Save Changes.
  2. Create a new field in the field editor(WP-Admin >> Event Manager >> Field Editor).
  3. The field created should be with the meta key value( _event_country).
  4. The placeholder should contain the same value, as provided in the code( de | in | us).
  5. Save changes.
  6. Link your Events with the country and finally you can filter them on the frontend.

    • Related Articles

    • Add a custom field in the search form on the event dashboard

      In order to add customize search filters to the event dashboard, please enter the following code in the functions.php file: add_action('event_manager_event_dashboard_event_filter_start', 'my_theme_custom_filter'); function my_theme_custom_filter() { ...
    • Creating a custom event search form

      WP Event Manager’s [Events] shortcode is coded to pick up search_location and search_keywords variables from the query string. Because of this, it’s easy to create a search form elsewhere on your site which posts to your events page. How to Create a ...
    • Remove existing field from event submission form

      Removing fields from event the event submission form In this guide, you will learn about how you can remove existing fields from the event submission form and how you can remove some social fields from the organizer section. Remove existing field ...
    • Create or pick categories from Free/ Core WP Event manager

      Events are classified into various types based on the kind of visitors they have. Visitors come from different backgrounds. That is why an event organizer or admin must ensure that they make a lot of event categories available to their clients. This ...
    • Hide and Add column from the Event Dashboard

      An event dashboard displays all the details related to a specific event. A user can easily access the dashboard after logging into his or her account on WP Event Manager. The event dashboard column can be seen directly in the home page. A user can ...