My content type has a taxonomy reference and a view reference field. I currently want to pass the Term ID as an argument into my referenced View via a Contextual filter Taxonomy Term. If someone can provide me with an example of how to write the PHP code in the contextual section of my view that would be great.

enter image description here

1 Answer 1


As the help text puts it, you just need to return the value. Of course, your actual question might be to access the term id but you have not specified that.

For example, if you want to show the view as a block on a regular node page, and the node contains the tid, you would use code like this:

// Lets assume that this view will always be shown on a node page.
$node = arg(1);
$tid = $node->field_term_reference[0][LANGUAGE_NONE]['value'];
return $tid;

This code could be improved a lot but it gives the basic idea.

As a side note, if your scenario is as per above, then consider using Panels and pass in the term id using contextual arguments.

Side note 2: Your screenshot shows filter dialog for Term Name whereas your question asks for term ID. I have given the example for the latter. I hope it helps get you started.

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.