By ya.teck


2011-03-25 17:26:34 8 Comments

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

5 comments

@Nevos 2011-09-22 09:52:26

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.

@Sivaji 2011-03-25 18:17:03

  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'),
);

@Temaruk 2011-09-08 16:55:27

What to do if you would like to get the node counts of the terms attached to the actual node?

@chrowe 2011-12-31 17:17:26

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

@colan 2013-08-06 14:34:00

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.

@Chunty 2011-11-24 16:01:13

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

@michaelmcandrew 2016-03-07 16:54:24

This would work much better as an edit to someone else's answer (or you could write a more complete answer).

@tim.plunkett 2011-03-25 17:32:52

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'.

@ya.teck 2011-03-25 17:42:26

I have views 7.x-3.0-alpha1. Where is the 'contextual filter'.

@tim.plunkett 2011-03-25 18:04:20

Then it is labeled "Arguments". However, you should probably just upgrade to the latest version.

@ya.teck 2011-03-25 18:43:54

3.0-alpha1 is the latest version views for drupal 7. Do you mean upgrade to the dev version?

@tim.plunkett 2011-03-25 19:47:52

The alpha1 was obsolete on January 6th. There should be an alpha2 this weekend, but yes, use the dev.

@Alexander Kim 2011-07-16 04:06:28

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?

@Alexander Kim 2011-07-16 04:08:33

So i guess it displays TERM COUNT isntead of NODE COUT :)

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Taxonomy term count per vocabulary

  • 2014-12-08 22:54:37
  • herci
  • 621 View
  • 1 Score
  • 1 Answer
  • Tags:   7 taxonomy-terms

1 Answered Questions

Views Taxonomy selection and display

1 Answered Questions

[SOLVED] How to sort terms in Views by their weight

  • 2014-11-21 11:32:38
  • user38998
  • 990 View
  • 1 Score
  • 1 Answer
  • Tags:   views

2 Answered Questions

[SOLVED] Views: How to display a grouped taxonomy list with node count?

1 Answered Questions

1 Answered Questions

[SOLVED] Taxonomy Terms node count of different content type

  • 2013-12-12 07:43:07
  • Mudasir
  • 754 View
  • 5 Score
  • 1 Answer
  • Tags:   taxonomy-terms

Sponsored Content