So I'm trying to create a View, displayed in a Block, that will show a list of nodes that reference the same parent node as the node I'm currently in. The reference is a custom field using the References module. I'm on Drupal 7 with Views 3.

I figure I'll setup a Contextual Filter on my custom field (field_ref_photo_gallery) and I need to provide a default value via PHP code.

How should I write that PHP code in the most Drupal 7-ish way?

Should I go with something like:

return node_load($nid)->field_ref_photo_gallery['und'][0]['value'];

(I'm not even sure that would work. I just threw that together in this post)

Or is there a field API function I should be calling?

  • can you use taxonomies in your case? Taxonomies have a built-in "depth" option that will allow you to reference the parents
    – uwe
    Commented Oct 24, 2011 at 20:18
  • did you solve this?
    – headkit
    Commented Aug 13, 2012 at 10:10
  • Unfortunately I can't remember which direction I went with this :( I do now know that you should always use field_get_items() or field_view_field() when you want to reference a field attached to an entity.
    – JonMcL
    Commented Aug 13, 2012 at 14:20
  • There will be a contextual filter on your reference field which takes the nid in the url given that you have this block only on a particular node page.
    – SGhosh
    Commented Jan 25, 2013 at 11:20

3 Answers 3


You can pass currently viewing nodes id in contextual filter without using any custom code.

In contextual filter settings, under When the filter value is NOT in the URL select Provide default value option. Then there is a dropdown will show. Then select Content ID from URL. See the image:

enter image description here

  • I have long since forgotten why I had trouble with this question. I have since done many similar Views and your answer is the method I currently use to develop them. There may have been some sort of uniqueness to my original issue, but I've since forgotten what it was. The "Content ID from URL" choice is very powerful and very poorly labeled.
    – JonMcL
    Commented Dec 31, 2013 at 16:05

In views 3 the "Contextual Filters" is located in the "Advanced" field set. Adding contextual filters also known as arguments will help display the appropriate content. One may also use "Relationships" in conjunction to add more fields or filters to your view.


Try views_php module to add custom PHP code on views filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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