Excluding the current post from a view

Excluding the current post from a sidebar view is something that I sometimes like to do.

Case: It’s nice to have a view of, say, “related content” that takes the page’s terms as an argument, and shows all of the nodes tagged with the same terms as the node we’re on. We build a block view to do this*, put it in a sidebar, and activate it.

Problem: A list of related nodes on each page, including the current page we’re already on.

Desired Outcome: Wouldn’t it be nice not have the current post we’re on included in the resulting list?

It’s one of those little details that I think really makes a subtle difference. Showing the node we’re already on is an unnecessary duplication, and it’s a bit ugly. Likely if this tweak is done, it’s going to go unnoticed by the user — but when it’s not done, it is noticed. We can say the same of all the best UIX tweaks, can’t we?

Luckily, it’s simple to do. Simply add the node as an argument, and then exclude it.

Step by step:

1. Add a Node: Nid argument.

Add Argument Node: Nid

2. Select “Provide Default Argument.”

3. Choose “Node ID from URL”

4. Select “Exclude this argument.”

5. ???

6. PROFIT!

Caveats: There is one caveat with doing this, however: caching. If you need a heavily cached site, then regenerating a view per page, which is what you’ll force it to do, can add a bit of overhead.

Basically, you won’t be able to set the view to cache at all using the native per-View cache; however, if you use it in a block (or with Panels Context) then you can set the block to cache per page if you use the CacheRouter module (which I recommend everybody use, just ’cause it’s awesome) and/or cache per page in your per-pane Panels cache.

* …using “provide default argument if none exists”, if it’s a block — because blocks can’t take arguments. It will snag the $node object on the current page automatically.