Filtering a Drupal 7 View based on a user profile field

The exact question answered here is “How do I filter a View based on the value of a taxonomy term reference field in the current user’s profile?”

This is covered in quite a few places on the web, but I just wanted to create good step-by-step instructions on how to actually do it, in a place where I can find the instructions easily. So they’re here:

We’ll start by assuming that we have already created a Taxonomy vocabulary called “Fruit”, with some terms as well. The Article content type should have a term reference field referring to the Fruit vocabulary, as should the user account.

1) Create a new View

– Admin -> Structure -> Views -> “+ Add new view”
– Name “My test view”
– Show “Users”
– Defaults are fine for the rest, click on “Continue & edit”

2) Add relationships

– “> Advanced”
– “Relationships add”
– “User: Fruit (field_fruit)”
– “Apply”
– The default identifier is fine
– Check “require this relationship”
– “Apply (all displays)”

– “Relationships add”
– “Taxonomy term: Content with term”
– “Apply (all displays)”
– “Relationship: term from field_fruit”
– “Apply (all displays)”

3) Add contextual filter

– “Contextual filters add”
– Check “User: Uid”
– “Apply (all displays)”
– Select “Provide default value”
– Type “User id from logged in user”
– “Apply (all displays)”

4) Define fields for the view
– Under “Fields” click on “User: Name”
– “Remove”
– “Fields add”
– “Content: title”
– “Apply (all displays)”
– Defaults are fine, “Apply (all displays)”

Easy as pie! Right?

Edit: If you want to filter by multiple terms so that all terms must match, see my next post.

This entry was posted in IT and tagged , . Bookmark the permalink.

14 Responses to Filtering a Drupal 7 View based on a user profile field

  1. Juan says:

    Sounds easy, but I followed the steps and there is no option to display the “Content: title” field, maybe because it’s a user view (all the listed fields are user or taxonomy related, not content related).

    Basically the same steps are recommended here http://drupal.org/node/1111722#comment-4315620 and it seems to work for some users, so maybe it’s a problem of my configuration or the versions I use (Views 7.x-3.5+17-dev and Drupal 7.15 in my case).

    Can you help me?
    Thanks in advance!

    • amplifier says:

      Sounds like there might be something wrong with your relations, especially the one connecting the taxonomy term to the nodes. Also, have you tried the latest stable version of Views? Is there a specific reason why your using the dev version?

  2. Juan says:

    You are correct, there was a problem with my relationships’ configuration (entirely my fault). It was really “easy as pie” indeed! 🙂
    Thanks for your help and sorry for wasting your time…

  3. Juan says:

    Now I’m trying to do the same thing but for several terms simultaneaously. Following the example, I would like to show the content that match the kind of Fruit selected by each user, and also (at the same time) other criteria/taxonomies selected: Color, Size or whatever.
    I’ve tried applying the configuration described in your article to all terms, but it doesn´t work, because the “Content: title” field only allows to select one relationship (and completely ignores the rest, even if marked as required).
    I’ve also tried creating a chain of relationships like suggested by Merlinofchaos here: http://drupal.org/node/672002#comment-2425152
    I’m out of ideas. Do you have any ideas to make it work?
    Maybe the Relation or Entity Reference modules could help?

  4. amplifier says:

    Which operator do you want to use when filtering with multiple terms, AND or OR? In other words, do you want to get the content where both terms match or the content where either of the terms match?

  5. Jt says:

    What if my view is of users? When I follow these instructions I get no results.

    I have a taxonomy term of Groups – group 1, group 2, etc, and child terms of tiers – tier 1, 2, and 3. I want to show a view showing user fields from all the users in the currently logged in user’s entire group, and then a similar view of their tier.

  6. Imran says:

    Hi,

    I have two profile types teacher and students. They both a taxonomy field “State”.

    Below each teacher’s profile I want to show students from his State.

    I have added three relationships:

    1) User: Profile
    2) Field: State (which is a taxonomy term field). Have selected the required relationship check box.
    3) Taxonomy term: Profile using State.

    But still it displays all students’ profile from different states.

    Can you please tell where I am going wrong? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *