I am getting results as expected using Views UI. But I don't want to display a particular record from the output of Views UI. How to achieve this and will I require any hooks for this?

For example; My Views return result as 1,2,3,4. Now I don't want to show 1 in the result. I cannot change the query using hook_views_query_alter as 1 is being used in "where" clause.


An answer to this question very much turns on what record you want to exclude from the listing. But the typical route here is to use a Contextual Filter on your view.

A typical pattern is that you are displaying a View alongside a node, e.g. a view of items that are related to that node. In this case, you want to exclude the node itself. For this you create a contextual filter that provides a default value of the the current node when there is no filter value present in the URL. You then check the exclude box in the contextual filter configuration. (Detailed steps below.)

Update In comments, you give more detail about what you want to exclude. Or, put another way, what you want to include. In general terms, you want to include nodes that share ANY taxonomy terms with the current node. Specifically, if you have a Teacher content type and a Subject taxonomy, then your requirement is to show every teacher that shares ANY of the taxonomy terms (representing subjects taught) of the currently displayed teacher.

Contextual Filters can do this out of the box, although the configuration is not especially intuitive. The approach is to add a Content: Has taxonomy term ID contextual filter and then configure it so it pulls taxonomy terms out the current node. (Detailed steps below.)

Stage one: Exclude current node

  1. Add a contextual filter Content: Nid (under ADVANCED to the right of the Views configuration page).

  2. Under WHEN THE FILTER VALUE IS NOT IN URL select 'Provide a default value' and choose 'Content ID from URL'. (This gets the node id.)

  3. In the MORE section check the box 'Exclude'. (This says instead of filtering to show only the node, filter to show anything but the node. Of course, the normal non-contextual filters also apply.)

You can add more than one Contextual filter to build up more complex conditions. And, as noted above, you can write PHP for the Contextual filter.

Step 2

Step 3

Stage two: Include all nodes that share ANY of the taxonomy terms of the current node

(aka, exclude any nodes that don't have any common taxonomy terms with the current node, but include everything else)

  1. Add a contextual filter Content: Has taxonomy term id.

  2. Under WHEN THE FILTER VALUE IS NOT IN URL select 'Provide a default value' and choose 'Taxonomy term ID from URL'. (This is a powerful option and what it does depends on the next configuration steps.)

  3. Uncheck the 'Load default filter from term page' box.

  4. Check the 'Load default filter from node page, that's good for related taxonomy blocks'

  5. You will usually want to check the 'Limit terms by vocabulary' box and then select the relevant vocabulary, e.g. 'Subjects'. You might skip this step if you know you only have term reference field on kinds of nodes you are interested in. But it does not harm to check it even in these cases.

  6. Under Multiple-value handling select the 'Filter to items that share any term' option.

  7. Further down the configuration find the 'Reduce duplicates' check box and check it.

  8. Open up the MORE fieldset at the bottom of the configuration and check 'Allow multiple values'

stage two: illustration a stage two: illustration b

  • I need to provide the content Id in my where clause. The scenario is as follows. Suppose A,B,C,D,E are Teachers and Subjects are Physics,Maths,Biology,Chemistry.Now the relations of subject and teachers A,B,C,D,E are as follows: - A teach Physics,Maths,Biology. B Teach Physics,Biology, C Teach Chemistry,Physics, D Teach Maths Biology, E Teach Chemistry.User comes to visit Teacher A .Now I want to display all the teacher who teach the subjects like A. So in my case B,C,D will be filtered because they have one or more subjects in common with A. – simple user Mar 29 '13 at 18:45
  • But Teacher E will be discarded as the subject is not common with A.In this scenario I need to give "ContentID"="A" in where condition and I don't want to display A again in the view. So the view will show B,C,D instead of A,B,C,D. I hope I am clear with my requirement. Please help me in achieving this – simple user Mar 29 '13 at 18:46
  • @user1843970 The extra detail you provide is helpful (and would be worth adding to the original question for other users). Based on extra detail I've updated my answer. I've tested the configuration with your sample data and get the specified results. And when you view, say, teach E, you are only shown teacher C; when view C, you are shown A, B and E. That is, it show all teachers with one or more subjects in common. – Obliquely Mar 30 '13 at 9:22
  • But I am using Node Reference Field instead of Taxonomy to relate Subject and Teachers. Subject Physics,Maths etc are nodes of "Subject" Content Type. I created it as nodes because there are many additional fields in it. I got your solution in case of Taxonomy but it doesn't work for node reference. – simple user Mar 30 '13 at 15:49
  • Is there a downside to simply adding extra fields to your subject vocabulary, rather than using a node? That's definitely a strategy to consider and it does mean you get various useful for behaviour for free or for the cost of (as in answer I've given) a bit of mildly fiddly configuration. If you need to go the Node Reference Route, you'll be able to reach your goal using a Contextual Filter. But I suspect you'll need PHP and that you will be, as it were, reinventing a wheel. (If you want help with this, I suggest modifying your question to narrow down your requirement.) – Obliquely Mar 30 '13 at 16:53

I created sample block that display 5 articles (titles).

One way of ommit first article is go to:

Pager -> User pager and click on numers of element, than:

enter image description here

Specify offset to 1 to the job

  • I didnt get your solution. Can you please be more elaborate?The working view is being displayed inside a block. – simple user Mar 28 '13 at 17:30
  • @user1843970 Yes I will improve my answer – Codium Mar 28 '13 at 17:31
  • I cant do so because the record may come in any place due to dynamic sort criteria. I cannot handle offset as it can lead to loss of other records as well. – simple user Mar 29 '13 at 5:57

If there is something specific about the record you don't want to return, you can filter your results so that it excludes that record. For example, if you're returning nodes and you don't want to return the node with NID = 1, then add an entry to Filter Criteria where Content:Nid "is not equal to" the value of 1.

  • Well if I provide a fixed number as you mentioned then my problem is solved. But I cannot do so. I need to pass the content ID from URL to Content:Nid in Filter Criteria. Is there any way to pass dynamic filter criteria? – simple user Mar 29 '13 at 5:55
  • As @Obliquely said, Contextual filters are the way to go in this case. – Cassandra Gelvin Apr 1 '13 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.