I want to remove "where" clause for city and postal_code in the views query. I found an example online for D6. I tried to change it a bit but it's not working. The "where" clause is still there. Below is my code. It was originally for D6.

function distsearch_views_query_alter(&$view, &$query) {
  if ($view->name == 'search' && $view->current_display == 'page_3') {
    // Remove the exposed filter conditions from the WHERE clause (WHERE city = MyCityInput)
    // and remove the corresponding args that were passed via the exposed filters
    // This effectively turns the city and zip code exposed filters into just text fields
    // that get used in distprox_mini_exposed_filter_proximity();

    $city = $view->exposed_raw_input['city'];
    $zip = $view->exposed_raw_input['postal_code'];

    // Remove the city or zip arguments (from the exposed filters)
    foreach ($query->where[0]['args'] as $key => $arg) {
      if ($arg == $city || $arg == $zip) {

    // Remove the corresponding WHERE clause additions
    foreach ($query->where[0]['clauses'] as $key => $clause) {
      if (strpos($clause, 'postal_code') !== FALSE || strpos($clause, 'city') !== FALSE) {

It was a lot easier than I thought. I dsm the query and did it like below. Not sure it's the perfect way to do it but it's working.

function distprox_mini_views_query_alter(&$view, &$query) {
  if ($view->name == 'search' && $view->current_display == 'page_3') 
| improve this answer | |
  • Aside from the obvious "just remove it from the UI" that I assume is not a solution for you, at first glance this looks like the right way to do it. – Letharion May 9 '12 at 19:29
  • I'm trying to create a search by city and zip that will return other towns and zip codes within the miles entered by user. I already changed to query to use longitude and latitude. If I leave the where clause for city and zip, the search result will be limited to that particular city and zip. I got the idea from this great tutorial agileadam.com/drupal-city-zip-proximity. – Dee May 9 '12 at 20:17
  • One caveat, in my case I noticed that if you unset a condition on a field where some fields are set with a value on the actual nodes and some aren't, the only ones that will show up after the unset are ones with set values - weird. – cdmo Mar 2 '18 at 20:45
  • UPDATE: so if the filter you're trying to unset is a field present on a related entity.... you may want to remove the table too: unset ->table_queue['table_name']. Wish there was a nice helper function for this, though I get it's uncommon. – cdmo Mar 2 '18 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.