Exclude the current node from a list view

Last modified: September 10, 2009 - 02:39

In some situations, for example a block listing nodes related to the node being viewed, you might wish to exclude the current node from a list view.

Using Views module, add a "Node:ID" argument type to your view. In the Option dropdown, select "Not Equal". Then place the following in the Argument Handling Code:

if ( ($type=='block') && is_numeric(arg(1)) ){
  $args[0] = arg(1);
}

return $args;

Adapted from this forum post: http://drupal.org/node/131482

Drupal 6 and higher

For Drupal 6+ (i.e. Views 2) no PHP is needed. http://drupal.org/node/328104#comment-1119773 gives the following method:

Use views arguments, assuming this is a block view

1. Add an argument for Node: nid
2. Set 'Action to take if argument is not present' to 'Provide default argument'
3. Set 'Default argument type' to 'Node ID from URL'
4. Check 'Exclude the argument'

 
 

Drupal is a registered trademark of Dries Buytaert.