PHP Snippet Block for OG group posts

Last updated on
30 April 2025

Here is some code I used for a block to display nodes that have selected their audience as an organic group. There is probably a million other ways to do this. This was just a quick fix.

The content type I used for my organic groups is a 'community'.

This is the code I put in the block (after enabling a php input filter):

$cur_nid = arg(1);
   
   $output = '';
   //query the database
   $result = db_query("SELECT og.nid, n.title FROM {og_ancestry} og JOIN {node} n ON og.nid = n.nid 
   WHERE og.group_nid = $cur_nid");
   while ($rows = db_fetch_object($result))
   {
       $output .= '<li><a href="../node/' . $rows->nid . '">' . $rows->title . '</a></li>';
   }
   if ($output == '')
   {
      echo 'No posts found that belong to this community.';
   }
   else
   {
      echo '<ul>' . $output . '</ul>';
   }

This is the code I put in for the visibility settings on the block (if this code return true blah blah blah):

$cur_nid = arg(1);

if (is_numeric($cur_nid))
{
   $result = db_query("SELECT n.type FROM {node} n WHERE n.nid = $cur_nid");
   while ($rows = db_fetch_object($result))
   {
      $cur_type = $rows->type;
   }
   if ($cur_type == 'community')
   {
      return true;
   }
}


Help improve this page

Page status: Not set

You can: