{"id":73,"date":"2020-01-28T18:52:00","date_gmt":"2020-01-28T18:52:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=73"},"modified":"2024-02-06T08:14:24","modified_gmt":"2024-02-06T13:14:24","slug":"creating-a-drupal-view-that-sorts-by-count","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/creating-a-drupal-view-that-sorts-by-count\/","title":{"rendered":"Creating a drupal view that sorts by count"},"content":{"rendered":"\n<p>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 &#8220;operating systems&#8221; with terms linux, mac, and windows each being used in 70, 30, 33 content types. You want to sort them as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>linux 70\nwindows 33\nmac 30<\/code><\/pre>\n\n\n\n<p>Following are the steps<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Structure &gt; Views<\/li><li>Add new view<\/li><li>Give a name. Show=taxonomy terms. type=operating system. Choose block (you can also choose page, block or both based on your requirements). Continue &amp; edit<\/li><li>There should already be a FIELD &#8220;Taxonomy term: operating systems&#8221;.<\/li><li>Add a RELATIONSHIP, &#8220;Taxonomy term: Content with term&#8221;. RELATIONSHIP is found under Advanced.<\/li><li>Create an Aggregation &#8220;count&#8221;. Also under Advanced<\/li><li>Create a new FIELD Content:Title. You should see &#8220;(node) count(Content:Title) | Aggregation settings&#8221; when you are done. Make sure the aggregation settings are count.<\/li><li>Filter should be &#8220;Taxonomy vocabulary: Machine name (= Technologies) | Aggregation settings&#8221; with aggregation setting count<\/li><li>Sort criteria should be &#8220;(node) COUNT(Content: Title) (desc) | Aggregation settings&#8221; with aggregation setting count<\/li><\/ol>\n\n\n\n<p>Save and verify.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[21],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-drupal","tag-drupal"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/73\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}