Exclude the current node from a list view

bumathan - March 27, 2007 - 15:51

Hello,

I've build a view that display "other nodes in the same category". So this little block is displayed
near the full node the user is currently reading and display a list of teasers of some other nodes of the same type. The problem is that the list includes the current node (the full one). I'd like to remove it from the list.

Do you know what is the code or the arguments to select in the ''View configuration'' to exclude the current node from the list ?

Any help would be nice.

Thank you very much.
Matt

Try this...

aether - March 27, 2007 - 16:01

Assuming you are 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;

Cool !

bumathan - March 27, 2007 - 16:54

Thank you very much

I'd like to know one thing,
is it possible to use this code in a block created manually ?

How could I write it ? To say, if the node inside the block is the same as the current one, don't display this block at all.

Thank you.

You are welcome.

aether - March 27, 2007 - 17:07

Your new question is beyond my current level of expertise. Hopefully someone else here can help you with that. Best of luck!

If you did it this way...

emdalton - March 27, 2007 - 17:56

You might have used this snippet or something similar to create your block:

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
 
$terms = taxonomy_node_get_terms(arg(1));

   
$output .= "<ul>";
    foreach(
$terms as $term){


   
//$taxo_id = 7;
   
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $term->tid LIMIT 5";
    
   
$result = db_query($sql);
    while (
$anode = db_fetch_object($result)) {
         
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
    }
    
    }
   
$output .= "</ul>";
    return
$output;
}
?>

If so, you want to add an if statement within the while loop, so the $output only gets added to if arg(1) is not equal to $anode->nid, but I'm too new with PHP to be able to tell you exactly how to code this.

Hope this helps!

use in the sql

nirl - April 23, 2007 - 18:12

actually you should probably check for the node existance in your SQL. something like:

$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $term->tid and node.nid != " . arg(1) ." LIMIT 5";

or whatever way you do this kind of stuff in PHP...

SQL Injection Attack

rhys - June 9, 2007 - 11:30

You have to be careful to check that arg(1) is a number. With a well-crafted URL, it would be possible to do an SQL injection attack on your system. I'm not sure if this applies to blocks, but assuming you have the view with a URL, and which takes arguments, then it is possible to accomplish.

Great!!

mr.cake - September 5, 2007 - 15:43

Thank you very much jtomlinson for this little snippet. I tried to work around a (probable) views-bug for some days now and this hint helped me out!! Thanks alot!

Perhaps anyone has an idea why this doesn't work? :

views_view_add_filter($view, 'node', 'nid', '!=', $nid, '');
views_sanitize_view($view);
print views_build_view('block', $view, array(), true, 10);

The error output could be found in this issue: http://drupal.org/node/171097
Basically the primary table name is missing within the sql-query...

work as a charm

mediamash - August 19, 2008 - 12:48

work as a charm

Prevent "Is one of..." filter from appearing on page

henigushi - May 23, 2007 - 13:59

Hi,

I am a drupal newbie and am very happy to have just completed my first list View. The only issue is that I have added a Node:Type filter, which works perfectly fine, but I don't want "Is one of..." appearing on the actual page.

I've scoured the drupal site for a topic addressing this but couldn't find one, which is why I'm posting here.

Can anyone help?

Much appreciated,

Henigushi

Exposed filter?

mr.cake - September 5, 2007 - 15:48

hi henigushi,
perhaps you exposed this filter (type)? The output would result in such a selectbox. remove this exposed(!) filter and it should disappear.

bookmarking

Jeff Burnz - September 22, 2008 - 17:16

bookmarking

How is this done in Views 2?

illuminaut - October 5, 2008 - 17:24

How is this done in Views 2? There is no options dropdown for the argument.

Hi, I do not remember where

Fayna - December 29, 2008 - 07:00

Hi,

I do not remember where I found this on drupal.org but here is what I did to have images within imagefields only display if associated with the current node:

1). Open up your view
2). Create an argument of Node > Node id.
3). Then choose the radio button option of "Provide default argument"
4). Some "Provide default argument" options should appear. Under "Default Argument Type," choose the radio button option for "Node ID from URL"
5). A little lower you should see "Validator options." Where it says "Action to take if argument does not validate," you can choose several different options from the drop-down menu. For example, "Hide View / Page Not Found (404)" if chosen will not display your view at all if say, no images were associated with the current node.

I hope that makes sense!

One more step

agaric - January 27, 2009 - 17:44

6). Just below that, checkmark Exclude the argument
"If selected, the numbers entered in the argument will be excluded rather than limiting the view."

We needed this on a view showing content related by taxonomy term to keep from claiming it was related to itself.

benjamin, Agaric Design Collective

Other nodes from the same category

luciana_andron - July 29, 2009 - 10:44

Hi,

I am new to Drupal and I want to create a block view that will display the list of other nodes from the same category of the current node view. Can you please tell me how you did it step by step, cause I am not familiar with using arguments in views....

Thank you!

Luciana

Same problem, but with pathauto enabled

dkane - September 13, 2009 - 02:11

I am looking to do the same thing, except with other image galleries (individual nodes) I've created. The "exclude the argument" answer is the one I keep coming across on the boards, however I am using pathauto so there is not a NID in the URL for the argument filter to look at.

For example, my path would be something like "photography/portraits" so I don't want the "portraits" thumbnail to show up in my "other galleries" block.

How can I exclude the current gallery I'm in? It seems like a many of these posts get me close, but not quite there, as most of them require the use of the NID, and I've tried both the built in version w/ Views 2 & the older PHP solution, but neither is doing it for me.

Thanks.

Internally Drupal uses the

nevets - September 13, 2009 - 02:20

Internally Drupal uses the un-aliased path, so even if you have aliased a path of the form node/{nid} Drupal (and views) sees the path in the form node/{nid}

hmmmm =/

dkane - September 13, 2009 - 03:11

Any idea why it might not be working then? I have set the following:

Arguments:
Node:Nid

Actions to take if argument is not Present:
Provide default argument

Default argument type:
Node ID from URL

then I check exclude the argument.

**** Revision *****

I stand corrected, I was running the block on a page view that was filtering from a "title" argument, so there was no Nid for the block to pull from in that URL, which is why it wouldn't work. Now I just have to figure out how to get the Nid from node displayed in the the page view to the block on that same page.

Sorry for the confusion. I've been away for a while...

Thanks!

Ok, another approach. Add a

nevets - September 13, 2009 - 03:32

Ok, another approach. Add a filter using the Views exclude previous module. When you set up the filter one of the options will allow you to exclude any viewed nodes which will exclude the node on the current page.

Wow, worked like a charm.

dkane - September 13, 2009 - 03:48

Wow, worked like a charm. This goes up there as reason 578 why I love Drupal and everyone who contributes to it.

Thank you so very much nevets, I've been dealing with this one for a while.

Much Appreciated.

 
 

Drupal is a registered trademark of Dries Buytaert.