--- og/og.module	2005-04-15 11:56:46.000000000 -0700
+++ og-with-hierarchy/og.module	2005-08-04 16:12:20.184135254 -0700
@@ -594,10 +594,37 @@
   print theme('page', $output);
 }
 
+function og_get_home_nodes($gid){
+  //Make our code hierarchy aware
+  $parents = array();
+  $group_list = "na.gid = %d"; //chunk of extra SQL if needed
+  if(function_exists('og_hierarchy_get_parents')){
+    $parents = og_hierarchy_get_parents($gid);
+    if($parents){
+      $groups = array_keys($parents);
+      $groups[] = '%d'; //add our own group
+      $group_list = "na.gid IN (" . implode(", ", $groups) . ")";
+    }
+    
+  }
+
+  $sql = "SELECT DISTINCT(n.nid), n.status " . 
+    "FROM {node} n INNER JOIN {node_access} na ON n.nid = na.nid " . 
+    "WHERE $group_list AND na.realm = 'og_group' AND n.status=1 " .
+    "AND n.type NOT IN ('og', 'image') " .
+    "ORDER BY n.sticky DESC, n.created DESC";
+
+  return db_query($sql, $gid);
+}
+
+/**
 function og_get_home_nodes_sql() {
+
   return "SELECT DISTINCT(n.nid), n.status FROM {node} n INNER JOIN {node_access} na ON n.nid = na.nid WHERE na.gid = %d AND na.realm = 'og_group' AND n.status=1 AND n.type NOT IN ('og', 'image') ORDER BY n.sticky DESC, n.created DESC";
 }
 
+**/
+
 //  when you view a group, you really see some facts about the group in a block and then a list of nodes affiliated with that group
 function og_view(&$node, $teaser = FALSE, $page = FALSE) {
   if ($teaser || !$page) {
@@ -606,8 +633,7 @@
   else {
     // get all the group nodes. we have to break this list into two steps in case user is not a member of the group and post is public
     // TODO: perhaps we can use db_rewrite_sql() to simplify this query. The problem is that we *must* join against node_access, even for admin users.
-    $sql = og_get_home_nodes_sql();
-    $result = db_query($sql, $node->nid);
+    $result = og_get_home_nodes($node->nid);
     if (!db_num_rows($result)) {
       drupal_set_message(t('No posts yet in this group.'));
     }
@@ -1105,8 +1131,7 @@
 function og_feed($gid) {
   $group = (object) array('nid' => $gid);
   if (node_access('view', $cond)) {
-    $sql = og_get_home_nodes_sql();
-    $result = db_query($sql, $gid);
+    $result = og_get_home_nodes($gid);
     while ($onenode = db_fetch_array($result)) {
       // make sure user can view each node
       if (node_access('view', $onenode)) {
