Community Documentation

Exclude the current node from a list view

Last updated September 10, 2009. Created by pwolanin on March 27, 2007.
Edited by bekasu. Log in to edit this page.

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'

Comments

Thanks, just what I have been

Thanks, just what I have been looking for!

****Me and Drupal :)****
Clickbank IPN - Sell online or create a membership site with the largest affiliate network!
Review Critical - One of my sites

I need a similar solution for

I need a similar solution for a taxonomy term: show a list of nodes EXCLUDING nodes tagged with the currently viewed taxonomy/term/x tag. Any suggestions?

Morningtime, a digital agency
Drupal services, web development, project management.

It is my understanding that

It is my understanding that this is not possible with the Views 2 UI. However, I accomplished something similar by modifying the query via the views_pre_execute hook added to an existing custom module. Not pretty, but not sure there is another way:

function action_user_inquiry_views_pre_execute(&$view) {
if($view->name=="provider_locations_node"
&& $view->current_display=="block_1") {
$view->build_info['query']=preg_replace('/ORDER BY location_node_data_field_primary_address_distance/',
'and users.uid not in(SELECT field_inquired_user_uid FROM content_field_inquired_user WHERE nid = '.arg(1).' ORDER BY location_node_data_field_primary_address_distance',
$view->build_info['query']);
}
}

content_field_inquired_user is a user_reference CCK field, and I want to only show users that are not associated to this field of the current node being displayed (represented by arg(1)). I would love it if there was another way. You could do a similar thing for the term -

where node.nid not in (select nid from term_node where tid='.arg(1).')

instructive

@kmadel: I needed to add a custom sub-query to the Views SQL and your instructive example saved me time--thanks!

John

Thank you. This helped me a

Thank you. This helped me a lot. I spent over two days looking for a solution.

For 6x

To show only nodes from the current node author,
- use uid as argument, then
- select "Provide default argument" then
- select "User ID from URL" and
- check "Also look for a node and use the node author" and save it.

Hope this helps someone

But then how do we not

But then how do we not display the current node in the created list of other nodes by the author? I tried excluding the argument as stated above but then I get nothing at all.

EDIT: Never mind, it just wasn't working in the Views preview is all. Worked fine when I saved the view and actually viewed the node. Thanks for the tip!

views preview (blah)

I was fussing with the Views preview just the other day. It wasn't displaying a block output correctly. I finally wised up and viewed the actual block on a page and it worked as expected.

Exclude current node and viewreference

Hi

this tips works well on preview but not in the node page after...
i include my view in my template via viewreference and this code:

<?php if ($node->field_liste_autres_vdl[0]['view']): /*Affichage conditionnel du bloc Autres sorties*/?>
         <div id="bloc-autres-vdl">
         <?php  print $node->field_liste_autres_vdl[0]['view'] /*Autres sorties selon taxo type de vdl*/ ?>
         </div>
                 <?php endif;?>

whats wrong ? thanks

Dev Server Ubuntu 10.04 LAMP PHP 5.2.10 ( 1 Laptop)
7 websites powered by drupal 6 - Hosted by OVH and Always Data

You can also do this like so:

1. Add 'node: nid' argument to the view.
2. Select 'provide default argument'
3. Select 'Node id from url'.
4. Check 'Exclude argument'

I don't remember if you can do this in D5 but in views2 and D6 this works the same.

it works on D6 , thanks

it works on D6 , thanks

Dev Server Ubuntu 10.04 LAMP PHP 5.2.10 ( 1 Laptop)
7 websites powered by drupal 6 - Hosted by OVH and Always Data

Works on D7 too

great and simple solution.

For Drupal 7

The above instructions were a little confusing for me using Drupal 7 + views 3. Here is what I ended up doing that worked:

1. Click on the "advanced" tab.
2. Click on add under "contextual filters".
3. Choose Content:nid.
4. Under "when the filter variable is not available, choose "provide default value".
5. From the drop down menu select "content id from url".
6. Scroll all the way down to the bottom of the window and click on the "More" link.
7. Click "Exclude"

I seem to be having issues

I seem to be having issues doing this in D7 with views 3. I have the exact setup as above and it works on a node page i.e. it excludes the node being viewed. However on say a normal page of another node type or a view, none of the items are displayed. The block is set to be visible using a sitewide context and if I remove the contextual filter altogether the items are displayed correctly on every page, albeit without excluding the node that is being viewed. Any ideas?

Hi, having the same problem.

Hi, having the same problem. Did you find a solution? Thanks.

Thank you!

Me helped!

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.