7 – Taxonomy filter on custom view is only showing terms with no children

I have a form that sets the value for parent terms in a taxonomy vocabulary (web_topic). The variable ‘search_topic_options’ is stored in the variable table with variable_set.

The form allows the user to select taxonomy terms and this will change the view with hook_views_query_alter and show content associated with the taxonomomy term. However, view only shows top level taxonomy with no children.

So if I have two terms

node/1 fruit >> apples
node/2 fruit >> bananas
node/3 fruit
node/4 animals >> cat
node/5 animals >> dog
node/6 animals

only node/3 and node/6 appear in the view.

In the function to build the form

     $vocabulary = taxonomy_vocabulary_machine_name_load('web_topic');
     $terms = taxonomy_get_tree($vocabulary->vid, 0, 1);
     $topic_options = array();
     foreach($terms as $term) {
       $topic_options($term->tid) = $term->name;
     $default_topics = variable_get('search_topic_options');

and the function

function MYMODULE_views_query_alter(&$view, &$query) { 
      $view->query->where(1)('conditions')() = array('field' => "taxonomy_term_data_node.tid", "value" => $topic_options, "operator" => "IN");