Last updated January 17, 2012. Created on April 6, 2010.
Edited by gnassar, LowMemory, Feet. Log in to edit this page.

Views PHP Filter is a powerful way to customize your Views result set -- but it can be a little difficult to jump into at first. Users have generously donated the following examples so that others can get a general idea of how it works and figure out how to adapt the filter for their purposes. Feel free to throw in more examples by creating a child page -- the more the merrier!

The "official" text on how Views PHP Filter works, from its README:

A new filter, labeled "Node: Node ID", will appear as addablein your Views filter lists.  If you add that filter, you'll note the "Options" dropdown will have two choices: "PHP code" and "ID list".  "PHP code" will take a snippet of PHP as its value (without the <?php and ?> bracket tags), which should return an array of integers representing node IDs.  The filter will evaluate the PHP, and filter on the numbers in the return array.  "ID list" will simply take a list of numbers, separated by commas, and filter those node IDs.  This is currently used mainly for testing, but may survive to release if someone finds it useful.

(The node ID list functionality did survive, it turns out.) Too confusing? Check out the following examples to get a better idea.

Looking for support? Visit the forums, or join #drupal-support in IRC.


jkibble’s picture

All of the current private message modules I've found privatemsg and pm_lite are pretty good but don't do what I really wanted or the way I wanted. I figured I would follow pm_lite's lead and make private messages as nodes but for an inbox I was having a heck of a time (using views) showing only messages directed towards me. arguments where obvious and did work but you can change the url to see other peoples private messages so having views do it seemed natural. With phpfilter and a bit of tweaking with your snippet and 3 hours of fiddling about finding out what I needed to get to this point I have finally managed it.

For anyone else that is thinking along the same lines I'm using comments to the pm node as the threading type. It's not yet perfect but one hurdle at a time.

the_prefect’s picture

Hi there,

I understand the code. Plus ... this is exactly what I need and I know how to fit the PHP to my exact requirements.

What I do not know, is ... where to put the snippet? There are so many places to paste PHP into Views and where I would need them most, there aren't any ;-).

So please, where would I pack that?

Thanks in advance.

antonisar’s picture

I think this is an example for

Zatox’s picture

Sure is!
Other examples available here: #690670: Documentation for viewsphpfilter