Article

Drupal 7 Views - How to sort by Grouping Field according to Taxonomy Term ordering, whether custom or by a specific order

Drupal 7 Views - How to sort by Grouping Field according to Taxonomy Term ordering, whether custom or by a specific order

Scenario: You have a Views page or block that needs to display contents which are grouped or categorized/classified by a taxonomy term. In my case I have a list of contents (content type sponsors) that needs to be grouped and displayed in this order: Major Sponsors, Partners, Sponsors. For example in above screenshot.

How to display and group content items by your taxonomy terms

  1. Under Fields, create/insert a field that points to your taxonomy terms; you may want to exclude it from display or use it, depending on your display requirement. This maybe a term reference you have configured in your content type.
  2. On your Views Format, click on Settings (example: Format: Fluid Grid | Settings)
  3. Under Grouping field, select the field that references to your taxonomy term/s (example: Content: Category, a term and field created for this requirement)
    Group content items by terms in a taxonomy
  4. Save.

How to custom or manual sort / order your groupings in Views

After this step you may find your groupings (terms) may not be sorted according to your requirement (whether alphabetical, priority, or any custom ordering). The ordering you may see at this point will depend on many factors and default settings - example, the term ordering (term ID, name) in your vocabulary taxonomy, or perhaps it has no sorting order set at all. Therefore, you may need to do the following to control ordering.

  1. Create a Relationship > Content: Taxonomy terms on node
    Drupal 7 Views - Relationships Content Taxonomy Terms on Node
  2. Under Filter Criteria, use this relationship and use Configure field: Taxonomy term: Rendered Taxonomy term
    sort criteria
  3. Under Sort Criteria, (term) Taxonomy term: Name (asc). This is the relationship created and used in the filtering. This ensures that any ordering you make in the term listing gets followed accordingly by ascending or descending.
    Taxonomy Term listing ordering
  4. Add any secondary sorting as needed. In this requirement, besides ordering by grouped terms, I need to ensure sorting in each group can be customized or sorted manually by user which follows a created sort/ordering weight field. And in case user does not enter any preferred weight sorting, will fall back to default title alphabetical sorting (third and last sort criteria)

Additional reference: https://www.drupal.org/project/views/issues/1239278#comment-6583454

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.