Index: similarterms_2/views/similarterms_handler_argument_node_nid.inc
===================================================================
--- similarterms_2/views/similarterms_handler_argument_node_nid.inc	(revision 2494)
+++ similarterms_2/views/similarterms_handler_argument_node_nid.inc	(working copy)
@@ -30,6 +30,7 @@
 
     $options['vocabularies'] = array('default' => array());
     $options['include_args'] = array('default' => FALSE);
+    $options['depth'] = array('default' => 0);
     
     return $options;
   }
@@ -59,6 +60,12 @@
       '#default_value' => !empty($this->options['include_args']),
     );
 
+    $form['depth'] = array(
+      '#type' => 'weight',
+      '#title' => t('Depth'),
+      '#default_value' => $this->options['depth'],
+      '#description' => t('The depth will match nodes tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get nodes that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).'),
+    );
   }
   
   function validate_arg($arg) {
@@ -121,6 +128,46 @@
       $tids[$row->tid] = $row->tid;
     }
     
+    if ($this->options['depth'] != 0) {
+      if (count($tids) == 1) {
+        $placeholder = " = %d";
+      }
+      else {
+        $placeholder = " IN (" . implode(', ', array_fill(0, sizeof($tids), '%d')) . ")";
+      }
+
+      $subquery = "\n  SELECT tn.tid FROM {term_node} tn\n";
+      $where = "  WHERE tn.tid $placeholder\n";
+      $args = $tids;
+      $last = "tn";
+
+      if ($this->options['depth'] > 0) {
+        $subquery .= "    LEFT JOIN {term_hierarchy} th ON th.tid = tn.tid\n";
+        $last = "th";
+        foreach (range(1, abs($this->options['depth'])) as $count) {
+          $subquery .= "    LEFT JOIN {term_hierarchy} th$count ON $last.parent = th$count.tid\n";
+          $where .= "    OR th$count.tid $placeholder\n";
+          $args = array_merge($args, $tids);
+          $last = "th$count";
+        }
+      }
+      else if ($this->options['depth'] < 0) {
+        foreach (range(1, abs($this->options['depth'])) as $count) {
+          $subquery .= "    LEFT JOIN {term_hierarchy} th$count ON $last.tid = th$count.parent\n";
+          $where .= "    OR th$count.tid $placeholder\n";
+          $args = array_merge($args, $tids);
+          $last = "th$count";
+        }
+      }
+
+      if ($result = db_query($subquery . $where, $args)) {
+        while ($row = db_fetch_object($result)) {
+          // adding a key to ensure there aren't duplicates
+          $tids[$row->tid] = $row->tid;
+        }
+      }
+    }
+
     $this->tids = $tids;
     $this->view->tids = $tids;
     
