You are here

function hs_taxonomy_term_count_nodes in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 6.3 modules/hs_taxonomy.module \hs_taxonomy_term_count_nodes()
  2. 7.3 modules/hs_taxonomy.module \hs_taxonomy_term_count_nodes()

Drupal core's taxonomy_term_count_nodes() is buggy. See http://drupal.org/node/144969#comment-843000.

1 call to hs_taxonomy_term_count_nodes()
hs_taxonomy_hierarchical_select_entity_count in modules/hs_taxonomy.module
Implementation of hook_hierarchical_select_entity_count().

File

modules/hs_taxonomy.module, line 622
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_term_count_nodes($tid, $type = 0) {
  static $count;
  $term = taxonomy_get_term($tid);
  $tree = _hs_taxonomy_hierarchical_select_get_tree($term->vid, $tid);
  $tids = array(
    $tid,
  );
  foreach ($tree as $descendant) {
    $tids[] = $descendant->tid;
  }
  if (!isset($count[$type][$tid])) {
    if (is_numeric($type)) {
      $count[$type][$tid] = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND t.tid IN (%s)"), implode(',', $tids)));
    }
    else {
      $count[$type][$tid] = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids)));
    }
  }
  return $count[$type][$tid];
}