Title: Problem With custom Filter
Last modified: December 8, 2019

---

# Problem With custom Filter

 *  Resolved [raihanratul](https://wordpress.org/support/users/raihanratul/)
 * (@raihanratul)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/problem-with-custom-filter/)
 * HI,
    Thank you so much for the awesome plugin. I am trying to create a custom
   filter for city field. I have create a custom field called city with meta _city.
   I have used code from a previous thread [https://wordpress.org/support/topic/location-dropdown-event-filter/](https://wordpress.org/support/topic/location-dropdown-event-filter/)
   But the filtering is not working. Can you please let me know what else I have
   to make it work.
 * Here is the code
 *     ```
       add_action( 'event_manager_event_filters_search_events_end', 'filter_by_city_field' );
       function filter_by_city_field() {
       	?>
       	<div class="search_event_types">
       		<label for="search_event_types"><?php _e( 'City', 'event_manager' ) ?></label>
       		<select name="filter_by_city" class="event-manager-filter">
       			<option value=""><?php _e( 'Choose your city', 'event_manager' ); ?></option>
       			<option value="mumbai"><?php _e( 'Mumbai', 'event_manager' ); ?></option>
       			<option value="pune"><?php _e( 'Pune', 'event_manager' ); ?></option>
       			<option value="delhi"><?php _e( 'Delhi', '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_city_field_query_args', 10, 2 );
       function filter_by_city_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 city
       		if ( ! empty( $form_data['filter_by_city'] ) ) {
       			$event_city = sanitize_text_field( $form_data['filter_by_city'] );
       			$query_args['meta_query'][] = array(
       						'key'     => '_city',
       						'value'   => $city,
       						);
       		}
       	}
       	return $query_args;
       }
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fproblem-with-custom-filter%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Hitesh Makvana](https://wordpress.org/support/users/hiteshmakvana/)
 * (@hiteshmakvana)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/problem-with-custom-filter/#post-12217986)
 * Hi [@raihanratul](https://wordpress.org/support/users/raihanratul/)
 * First of all, you need to follow this doc for custom filter [https://wp-eventmanager.com/add-custom-field-in-search-filter/](https://wp-eventmanager.com/add-custom-field-in-search-filter/)
 * Your above code is incorrect please update following line in your code
 *     ```
       $event_city = sanitize_text_field( $form_data['filter_by_city'] );
       			$query_args['meta_query'][] = array(
       						'key'     => '_city',
       						'value'   => $event_city,
       						);
       ```
   
 * I hope this helps.
    Thank you
 *  [Hitesh Makvana](https://wordpress.org/support/users/hiteshmakvana/)
 * (@hiteshmakvana)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/problem-with-custom-filter/#post-12253671)
 * Hi [@raihanratul](https://wordpress.org/support/users/raihanratul/),
 * We haven’t heard from you in a while. For now, I am making this resolved. If 
   you still have an issue please create a new thread.
 * Thank you

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem With custom Filter’ is closed to new replies.

 * ![](https://ps.w.org/wp-event-manager/assets/icon-256x256.png?rev=1622826)
 * [WP Event Manager – Events Calendar, Registrations, Sell Tickets with WooCommerce](https://wordpress.org/plugins/wp-event-manager/)
 * [Support Threads](https://wordpress.org/support/plugin/wp-event-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-event-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-event-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-event-manager/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Hitesh Makvana](https://wordpress.org/support/users/hiteshmakvana/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/problem-with-custom-filter/#post-12253671)
 * Status: resolved