Using Drupal 7, Views 3 and the Node Reference module, I'm using a view to populate a node reference field on a custom content type. (So it's a view of "Reference" display type.)

I want to filter this view to show (and allow users to select) only:

  1. published content (OK)
  2. content of a certain type (OK)
  3. only nodes created by the current user (OK)
  4. OR if the current user is admin or some other role, bypass the previous filter (3) and show all nodes (but still respect filters 1 and 2) (not OK)

Filters 1 and 2 are always mandatory; then either filter 3 OR 4 must also be mandatory. I know I can rearrange filters into filter groups to make this work. The problem is that I cannot find a way to build filter 4.

For filter 3, I had to bring in a Relationship of type "Content: Author", which made a lot of new filters appear, including the "User: Current" that I used for filter 3 (node author == current user).

For filter 4, I need to filter based on the role of the current user (not author), and I cannot find how to do that.

In the filters list there's a new "User: Roles" filter available, but it only refers to the "Content: Author" relationship, so it only checks the node author's roles. That's not what I need: I need to check the current user's roles.

I'm guessing I have to add a new Relationship to bring in the current user data (something like "User: current"), and then filter on that data, but I cannot find that in the Add Relationship screen.

Any idea how to do that?

  • 2
    I did something along these lines here: drupal.stackexchange.com/questions/38205/…
    – Jimajamma
    Dec 31, 2013 at 2:13
  • @Jimajamma thank you very much, it does work, and I guess it's the best solution I will find! One question though: I want not only admin to have access to all values, but also all users of a certain role (only one role). Is it possible?
    – s427
    Dec 31, 2013 at 21:20
  • @Jimajamma Never mind, I think I figured out of to do it by myself. Thanks again!
    – s427
    Dec 31, 2013 at 21:48

2 Answers 2


Following @Jimajamma's suggestion, this is what I used. I adapted his code slightly for Drupal 7 and to allow for other roles than Administrators full access to the results.

In the View, add a Author: Uid contextual filter and use the following settings.


  • Provide default value

  • Type: PHP Code

    global $user;
    if (in_array('administrator', $user->roles) || in_array('editor', $user->roles)) {
      return -1;
    return $user->uid;


  • Specify validation criteria

  • Validator: PHP Code

    return ($argument != -1);
  • Action to take if filter value does not validate: Display all results for the specified field
    (This is what gives administrator users access to all results.)

That's it!

Compared to my initial settings, the Content: Author relationship isn't anymore needed, neither is the Author filter (which was brought in by the relationship).

Apparently, for Drupal 6, the condition in the first PHP snippet should rather be if (in_array('super user', array_values($user->roles))).

  • 1
    This answer really saved me after a whole day of frustration trying to find out the solution. Many thanks!!! :) Sep 22, 2016 at 8:56

You need to add a Contextual Filter to filter based on the currently logged in user.

  1. Add a contextual filter for "User: Uid"
  2. Select "Provide a default value"
  3. Choose Type: "User ID from logged in user"

You would also need to clone a new view display to show all nodes for specific roles, configured by Page Settings: Access > Role. Then choose Administrator and any other roles who should see all nodes.

You cannot OR a filter like a sort, and you would be displaying the view two different ways with filtered content and all content, so I would create two displays of this view.

  • 1
    Thanks for your answer. I hadn't paid attention to the "Access" setting of my view, which opens some interesting possibilities. The problem is that splitting the logic between two view displays isn't an option in my case, because I'm using that view to create a field (via the node reference module), so I can only use one view to create this one field which will be used on one content type (which will then be accessed by various types of users).
    – s427
    Dec 31, 2013 at 0:07
  • Note however that you definitely can "OR" filters (via the "rearrange" button). But not contextual filters, which maybe was what you meant. That is why I think the best approach would be via a relationship (to the logged in user), which would then give me "regular" (non-contextual) filters to use.
    – s427
    Dec 31, 2013 at 0:10

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.