Problem : You have organic groups installed and you have a content type that has been designated a group post (see the organic groups usage field for your content type). When you build a view to retrieve nodes of that content type you may be faced with a dilemma. When the view is used to retrieve nodes inside the context of a group you only want nodes that were created in that group; however you may also want to use the view to retrieve nodes that are not in any group i.e. you want to have your cake and eat it. [Note: there is an important distinction to be made here - nodes that are not in any group are different animals than nodes which were created in a group but which have been marked as public].

Solution : A method for solving this problem requires the latest version of Views (6.x-2.x-dev as of this writing) and the Views Or module. The reason you need the latest version of Views is that it contains an important new filter option Is empty - this is used to capture nodes that are not in any group. The reason you need the views_or module is that it allows you to combine filters with the boolean OR operator. [Note: the authors of views_or suggest that when the Views module is updated to include boolean OR in the future their module will become deprecated.] Why do we need a boolean OR? Because we need to create a view that returns either nodes that are not in any group or nodes inside the context of a group.

So, the filters to add and the order to add them in are as follows:

  1. Views Or: Begin alternatives
  2. Organic groups: Groups and set the operator to Is empty (NULL)
  3. Views Or: Next alternatives
  4. Organic groups: OG: Posts in current group
  5. Views Or: End alternatives

When you click the Preview button you should see a SQL query that looks something like:

SELECT node.nid AS nid, node.title AS node_title FROM node node  LEFT JOIN og_ancestry og_ancestry ON node.nid = og_ancestry.nid WHERE (og_ancestry.group_nid IS NULL) OR (og_ancestry.group_nid = ***CURRENT_GID***) ORDER BY node_title ASC

The condition on the left side of the OR is for nodes not in any group and the condition on the right side is for nodes in the context of the current group. You probably will have other conditions that narrow the scope of your view.

You can also import the following code into a view inside your site:

// import the lines between the asterisks into a new view
//*************************************************************************
$view = new view;
$view->name = 'content_in_context_or_public';
$view->description = 'Returns content that is either visible in the context of the current organic group or public (i.e. is not in any group)';
$view->tag = 'og';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'title' => array(
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
  ),
));
$handler->override_option('filters', array(
  'views_or_begin' => array(
    'id' => 'views_or_begin',
    'table' => 'node',
    'field' => 'views_or_begin',
  ),
  'group_nid' => array(
    'operator' => 'empty',
    'value' => array(),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'group_nid',
    'table' => 'og_ancestry',
    'field' => 'group_nid',
    'relationship' => 'none',
    'reduce_duplicates' => 0,
  ),
  'views_or_next' => array(
    'id' => 'views_or_next',
    'table' => 'node',
    'field' => 'views_or_next',
  ),
  'picg' => array(
    'id' => 'picg',
    'table' => 'og_ancestry',
    'field' => 'picg',
  ),
  'views_or_end' => array(
    'id' => 'views_or_end',
    'table' => 'node',
    'field' => 'views_or_end',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('items_per_page', 0);
//*************************************************************************