34

How can I display the node count values for taxonomy terms with sorting the result list in descending order of node count?

1
  • 2
    There's a video on youtube very well explained herehttps://www.youtube.com/watch?v=w22Utjm-chs
    – wranvaud
    Feb 29, 2016 at 23:20

5 Answers 5

42
  1. Create a taxonomy view
  2. Add node group in relationship
  3. Add term name and node nid fields for display
  4. Enable aggregation in "Other" section (Use aggregation: Yes)
  5. Click nid field settings icon, choose group type "count".

Find the export of views that worked for me.

$view = new view;
$view->name = 'term_node_count';
$view->description = 'Displays node count values for terms';
$view->tag = '';
$view->base_table = 'taxonomy_term_data';
$view->human_name = 'term_node_count';
$view->core = 7;
$view->api_version = '3.0-alpha1';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '100';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['pager']['options']['expose']['items_per_page_options_all'] = 0;
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'name' => 'name',
  'nid' => 'nid',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'name' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
  ),
  'nid' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
  ),
);
$handler->display->display_options['style_options']['override'] = 1;
$handler->display->display_options['style_options']['sticky'] = 0;
/* Relationship: Taxonomy: Node */
$handler->display->display_options['relationships']['nid']['id'] = 'nid';
$handler->display->display_options['relationships']['nid']['table'] = 'taxonomy_index';
$handler->display->display_options['relationships']['nid']['field'] = 'nid';
$handler->display->display_options['relationships']['nid']['required'] = 0;
/* Field: Taxonomy: Term */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['name']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['name']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['name']['alter']['trim'] = 0;
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['name']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['name']['alter']['html'] = 0;
$handler->display->display_options['fields']['name']['element_label_colon'] = 1;
$handler->display->display_options['fields']['name']['element_default_classes'] = 1;
$handler->display->display_options['fields']['name']['hide_empty'] = 0;
$handler->display->display_options['fields']['name']['empty_zero'] = 0;
$handler->display->display_options['fields']['name']['link_to_taxonomy'] = 1;
/* Field: Node: Nid */
$handler->display->display_options['fields']['nid']['id'] = 'nid';
$handler->display->display_options['fields']['nid']['table'] = 'node';
$handler->display->display_options['fields']['nid']['field'] = 'nid';
$handler->display->display_options['fields']['nid']['relationship'] = 'nid';
$handler->display->display_options['fields']['nid']['group_type'] = 'count';
$handler->display->display_options['fields']['nid']['label'] = 'Count';
$handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['nid']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
$handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['nid']['alter']['html'] = 0;
$handler->display->display_options['fields']['nid']['element_label_colon'] = 1;
$handler->display->display_options['fields']['nid']['element_default_classes'] = 1;
$handler->display->display_options['fields']['nid']['hide_empty'] = 0;
$handler->display->display_options['fields']['nid']['empty_zero'] = 0;
$handler->display->display_options['fields']['nid']['link_to_node'] = 1;

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
$translatables['term_node_count'] = array(
  t('Defaults'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort By'),
  t('Asc'),
  t('Desc'),
  t('Items per page'),
  t('- All -'),
  t('Offset'),
  t('node'),
  t('Term'),
  t('Count'),
  t('Block'),
);
1
  • What to do if you would like to get the node counts of the terms attached to the actual node?
    – Temaruk
    Sep 8, 2011 at 16:55
25

All the previous answers were very helpful.

I wanted to get a table so I could let the user sort by Term or by count.

To do this I:

  1. Relationship > Taxonomy term: Content with term
  2. Use aggregation: Yes
  3. Field > Content: Nid
  4. Aggregation type for Nid = Count DISTINCT
1
  • What's missing is how to get the two fields to show up in-line. For that, you need to go to Format -> Show -> Settings, and check both as in-line items.
    – colan
    Aug 6, 2013 at 14:34
12

To get a taxonomy node count correctly for taxonomy terms you need to do the following:

  1. In Fileds: add Taxonomy term: Name
  2. In Relationships, add a Taxonomy term: Content with term
  3. Add a contextual filter for Taxonomy term: Name

    3a. Choose 'Display a summary'

    3b. Choose 'Display Record count with link'. Also set base path if you want the taxonomy page to be linked correctly.

5

The key is that you have make sure you've ticked the "aggregation" check box in the advanced option of the view.

1
  • 1
    This would work much better as an edit to someone else's answer (or you could write a more complete answer). Mar 7, 2016 at 16:54
4

Add an argument (renamed 'contextual filter' in the latest Views) for taxonomy terms, and choose 'Display a summary'. Then, choose to display 'Number of records'.

6
  • I have views 7.x-3.0-alpha1. Where is the 'contextual filter'.
    – ya.teck
    Mar 25, 2011 at 17:42
  • Then it is labeled "Arguments". However, you should probably just upgrade to the latest version. Mar 25, 2011 at 18:04
  • 3.0-alpha1 is the latest version views for drupal 7. Do you mean upgrade to the dev version?
    – ya.teck
    Mar 25, 2011 at 18:43
  • 2
    The alpha1 was obsolete on January 6th. There should be an alpha2 this weekend, but yes, use the dev. Mar 25, 2011 at 19:47
  • Views 3.0-rc1. Tried as you posted above, but i only get for example: Category (1), Cats (1). Everywhere is 1 near taxonomy terms, though i have lots of nodes inside terms. What's wrong? Jul 16, 2011 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.