Skip to content

Instantly share code, notes, and snippets.

@nguyenthanhxuan
Last active August 29, 2015 14:24
Show Gist options
  • Save nguyenthanhxuan/aa5116b77d5ff53531a7 to your computer and use it in GitHub Desktop.
Save nguyenthanhxuan/aa5116b77d5ff53531a7 to your computer and use it in GitHub Desktop.
<?php
/**
* @param tid
* Term ID
* @param child_count
* TRUE - Also count all nodes in child terms (if they exists) - Default
* FALSE - Count only nodes related to Term ID
*/
function term_nc($tid, $child_count = TRUE) {
$tids = array($tid);
if ($child_count) {
$tids = array_merge($tids, term_get_children_ids($tid));
}
global $language;
$langs = array($language->language);
$langs[] = 'und';
$query = db_select('taxonomy_index', 't');
$query->condition('tid', $tids, 'IN');
$query->join('node', 'n', 't.nid = n.nid');
$query->condition('n.status', 1, '=');
$query->condition('n.language', $langs, 'IN');
$count = $query->countQuery()->execute()->fetchField();
return $count;
}
/**
* Retrieve ids of term children .
*
* @param $tid
* The term's ID.
* @param $tids
* An array where ids of term children will be added
*/
function term_get_children_ids($tid) {
$children = taxonomy_get_children($tid);
$tids=array();
if (!empty($children)) {
foreach($children as $child) {
$tids[] = $child->tid;
$tids = array_merge($tids, term_get_children_ids($child->tid));
}
}
return $tids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment