Taxonomy exposed filter with parent hierarchy only I have a taxonomy vocabulary with terms in hierarchical order. If I use an exposed filter, with level depth, all terms are listed, which are > 6,000. I want only the parent terms, which are only around 30.

I found here the following code (I adapted it to my case). It is an answered question, but obviously it does not apply efficiently to my case, because though it does what is suppose to do, it goes through every item and as a consequence is extremely slow (several seconds). Is there a better way that does not directly make a db_query to the taxonomy database tables or use a hook (PHP) ?

function mymodule_form_views_exposed_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if($form['#id'] == 'views-exposed-form-search-page-page'{
    foreach ($form['term_node_tid_depth']['#options'] as $term_key =&gt; $term) {   // Check if this isn't the 'ANY' option
      if($term_key !== 'All') {
        // Check if this is a child by looking for '-' as first char in string
        $term_value = reset($term-&gt;option);
        if($term_value[0] == "-") {
          unset($form['term_node_tid_depth']['#options'][$term_key]);
        }
      }
    }
  } // if
} // function
I had a situation like this, i done it by giving a custom options array to the exposed filter in hook_form_alter(). 

 function mymodule_form_views_exposed_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if ($form_id == "views_exposed_form" &amp;&amp; $form['#id']=='your-form-id' ){
 $tid_options = get_tid_options();

 $form['your-field-name']['#type'] ='select';
 $form['your-field-name']['#size'] = null;
 $form['your-field-name']['#default_value'] = '';
 $form['your-field-name']['#options'] = $tid_options;
   }
}


function get_tid_options() {
  $cc=array();
  $tax=taxonomy_vocabulary_machine_name_load('machine-name');
  $vid= $tax-&gt;vid;
  //Code to find parent terms here according to your vocabulary structure.
  $child_terms = taxonomy_get_tree($vid);
  foreach($child_terms as $values) 
  $tid=$values-&gt;tid;
  $name=$values-&gt;name;
  $cc[$tid] = $name; 
 }
return $cc;


}

Use taxonomy_get_parents or taxonomy_get_parents_all to find parent terms.

May be this will help you..