You are here

function taxonomy_edge_term_count_nodes in Taxonomy Edge 6

Reimplementation of taxonomy_term_count_nodes(). Avoid GROUP BY and recursive calls.

See also

taxonomy_term_count_nodes()

1 call to taxonomy_edge_term_count_nodes()
taxonomy_edge_taxonomy_term_count_nodes in ./taxonomy_edge.module

File

./taxonomy_edge.module, line 834
Selecting all children of a given taxonomy term can be a pain. This module makes it easier to do this, by maintaining a complete list of edges for each term using the adjecency matrix graph theory.

Code

function taxonomy_edge_term_count_nodes($tid, $type = 0) {
  static $count;
  if (!isset($count[$type])) {

    // $type == 0 always evaluates TRUE if $type is a string
    if (is_numeric($type)) {
      $result = db_query("SELECT COUNT(1) AS c FROM node n JOIN term_node tn ON n.vid = tn.vid JOIN term_edge e ON tn.tid = e.tid WHERE n.status = 1 AND e.parent = %d", $tid);
    }
    else {
      $result = db_query("SELECT COUNT(1) AS c FROM node n JOIN term_node tn ON n.vid = tn.vid JOIN term_edge e ON tn.tid = e.tid WHERE n.status = 1 AND e.parent = %d AND n.type = '%s'", $tid, $type);
    }
    $count[$type][$tid] = db_result($result);
  }
  return isset($count[$type][$tid]) ? $count[$type][$tid] : 0;
}