Sometimes you have to sort something by the count of its relationship or usage. For example, you need to sort a content type by it rating or you need to sort taxonomy terms by its usage in different nodes. The key to solving these problem is to create a relationship and then aggregate it. Aggregation basically binds fields together based on a criteria such as count. Following is an example that shows how to sort taxonomy terms by their usage in different nodes. Suppose you have taxonomy “operating systems” with terms linux, mac, and windows each being used in 70, 30, 33 content types. You want to sort them as follows:
linux 70 windows 33 mac 30
Following are the steps
- Structure > Views
- Add new view
- Give a name. Show=taxonomy terms. type=operating system. Choose block (you can also choose page, block or both based on your requirements). Continue & edit
- There should already be a FIELD “Taxonomy term: operating systems”.
- Add a RELATIONSHIP, “Taxonomy term: Content with term”. RELATIONSHIP is found under Advanced.
- Create an Aggregation “count”. Also under Advanced
- Create a new FIELD Content:Title. You should see “(node) count(Content:Title) | Aggregation settings” when you are done. Make sure the aggregation settings are count.
- Filter should be “Taxonomy vocabulary: Machine name (= Technologies) | Aggregation settings” with aggregation setting count
- Sort criteria should be “(node) COUNT(Content: Title) (desc) | Aggregation settings” with aggregation setting count
Save and verify.