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.

Views 3

  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"

Views 2

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'

If after doing either of the above you are still seeing the current node add another contextual filter for node revisions similar to the Node ID above.

Comments

Anonymous’s picture

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?

kmadel’s picture

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).')

johnhanley’s picture

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

John

ivanhi’s picture

Were you able to do it? I'm interested doing with Views 3 and Drupal 7.

Thank you

ivanhi’s picture

I have realized how to exclude a node with the same taxonomy.
In Contextual filter, select your field with the taxonomy vocabulary that want to exclude, and in the bottom check the box "exclude".

Thanks!

victoriachan’s picture

Currently the 'exclude' checkbox only shows up for 'numeric' and 'boolean' argument types. Hence it won't show on Taxonomy term name contextual filter, thought it does show on Taxonomy term ID filter (and you can then set the filter value type as 'Term name converted to Term ID' if you need to pass in the term name and not tid).

So yes, you can use the 'exclude' on taxonomy filters!

fehin’s picture

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

2noame’s picture

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!

johnhanley’s picture

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.

aiphes’s picture

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 Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

derekwebb1’s picture

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.

aiphes’s picture

it works on D6 , thanks

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

corneo131’s picture

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"

soulston’s picture

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?

rolturn’s picture

Just wanted to say THANK YOU!
These steps were so simple. I worked on this for hours before finding your 7 step process.

leewoodman’s picture

http://scito.ch/comment/27

Now you need to add the second filter in order to exclude the current node from the list. Click [+] button of the contextual filters section.
Select Content: Nid
Set the ‘when argument is not present’ to ‘Provide default argument’
Set the ‘Default argument type’ to ‘Content ID from URL’
Go to “MORE” , and check the “Exclude” box.
Move the filter “Content: Nid” to the top position.
Add a block to your view and embed the block on your node pages
Save your view

anjjriit’s picture

How to add filter if i want to display some node under node/nid from url ?
just for illustration:

node/nid from url is node/9
and i want to display node in views like this
node/8
node/7
node/6

thanks for your help

CD’s picture

Thanks, using D7, this worked, not problems!

jerry.xu’s picture

thanks, this is what i needed!

casti2525’s picture

Just what I needed!

DetectivePixel’s picture

Block works well in most pages besides the pages created by views were the block seems to disappear altogether.

By doing the following it seems to work as intended

In addition to following the steps for Views 3 do +

When the filter value is available or a default is provided

1. check -> Specify validation criteria
2. Select Basic Validation (or something else that makes sense for the situation)
3. Action to take if filter value does not validate -> Display All Results for the Specified field

It seems that views is passing some kind of argument or invalidates something?

Proteo’s picture

The proposed solution worked fine (the current node was excluded from the list). However, the block containing the view disappeared from every other page, and your suggestion worked like a charm, now the block works as expected in the rest of the site.

herinxi’s picture

This extra step worked for me with pages created by views. Thanks.

paddypatpat’s picture

Not sure why but I've gone though the steps carefully and whenever I apply the exclusion, it removes all of the content from the view, not just the one with the matching nid.

DetectivePixel's comment brings back the full set of results again, still no exclusion (which I think would make sense).

Suggestions?

CD’s picture

Hi,

Not sure if this helps but this is what I did when my whole view went missing.
On the front page the view did not appear, but when going to a content page the view appeared and worked to exclude the node I am on.
I fixed it by adding:

When the filter value IS available or a default is provided

Specify validation criteria

Validator

content

and then the content type(s) the view is filtered to show.

Now the view appears in full form on front page and works on the content pages it is set to exclude current node on.

jsheller’s picture

Thank you very much! Exactly what I needed!

WormFood’s picture

I'm using Drupal 7, and Views 3, with a contextual filter on the taxonomy term ID to display related content in a block.

The instructions here didn't work, until I unchecked "Specify validation criteria", which is selected by default. After that, it worked as expected.

And if there is only one node with this taxonomy term ID, it doesn't display the related content block at all; exactly what I wanted.

The more I learn about the finer details of Views, the more I love it.

Shashwat Purav’s picture

Worked for Drupal 7.39 like a charm. Thank you.

Thank You,
Shashwat Purav

rahulbaisanemca’s picture

Thank you so, this solution worked for me. Thanks again.

krina.addweb’s picture

I think we have pending steps which needs to be implemented:

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".
8. In Sort criteria add Global:Random field.
9. Save view & show result on a specific node.

Hope this helps you all!

ydahi’s picture

Many thanks, would have never looked in the More fieldset. Works like a charm!

codexico’s picture

How to do this in drupal 8?
It doesn't have the "Exclude" option under "More" :-(

codexico’s picture

Found it, in portuguese "id" is translated as "identificador", so I was triyng to use UUID instead, also tried also to use filters, finally the solution was to change to english, and there it was the "id" field and the option to exclude!

Bes2theFort’s picture

Thanks a lot for this awesome quick tutorial. You saved my day :)

nehapandya55’s picture

Thanks above solution worked for me.

Neeraj420’s picture

In my case exclude option is not working i have added Relation to my entity reference field as well.

hongqing’s picture

This is post is very useful. However, I do not see the "More" option in the CONTEXTUAL FILTERS. Is it removed from Drupal 9?

hongqing’s picture

Hi all, I found I cannot see the "More" potion in the CONTEXTUAL FILTERS because the window is small. You may find it on a bigger monitor or by zooming out the web page. Hope this is helpful for those having the same problem.