Get all nodes attached to a vocabulary,Get all nodes directly attached with a term id,Get all nodes belong to a term id recursively(parent-child taxonomy terms)

Public

Get all nodes attached to a vocabulary,Get all nodes directly attached with a term id,Get all nodes belong to a term id recursively(parent-child taxonomy terms)

</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * function to get all term ids inside a vocab in a flat array
  5.  * No matter if vocab consists of hierarichal order of terms,you will get all term ids in a flat array.
  6.  * @param
  7.  * $vocab_machine_name takes vocabulary machine name as parameter.
  8.  * @return
  9.  * $tids return array of tids in a flat array format
  10.  */
  11. function get_taxonomy_term_ids_inside_vocab($vocab_machine_name) {
  12. $vocab = taxonomy_vocabulary_machine_name_load($vocab_machine_name);
  13. if (is_object($vocab)) {
  14. $term_tree = taxonomy_get_tree($vocab->vid);
  15. if (is_array($term_tree) && !empty($term_tree)) {
  16. foreach ($term_tree as $key => $value) {
  17. $tids[] = $value->tid;
  18. }
  19. } if (empty($term_tree)) {
  20. $tids = array();
  21. }
  22. } else
  23. return "No Vocab Found with the given name";
  24.  
  25. return ($tids);
  26. }
  27.  
  28. /**
  29.  * function to get all the node ids directly mapped to the given taxonomy terms.
  30.  * Returns Flat array of node ids belong to the given term ids.
  31.  * @param
  32.  * $tid takes term id can be a single term or array of terms.
  33.  * @return
  34.  * $nid/$nids returns node ids array or sinly node id directly mapped to the given taxonomy term.
  35.  */
  36. function get_nodes_directly_mapped_to_term($tid) {
  37. if (is_array($tid) && !empty($tid)) {
  38. foreach ($tid as $key => $value) {
  39. $nids[] = taxonomy_select_nodes($value);
  40. $flat_nids = multitosingle($nids);
  41. }
  42. return $flat_nids;
  43. }
  44. else {
  45. $nid = taxonomy_select_nodes($tid);
  46. return $nid;
  47. }
  48. }
  49.  
  50. /**
  51.  * function to convert mutlidimension arrays to flat arrays.
  52.  * Returns Flat arrays.
  53.  * @param
  54.  * $input takes input array as multudimension array.
  55.  * $output takes output array as paramter in the recursive call.
  56.  * @return
  57.  * $output return flat array format for the input multidimenional array
  58.  */
  59. function multitosingle($input, $output = array()) {
  60. ksort($input);
  61. foreach ($input as $value) {
  62. $key = count($output);
  63. if (is_array($value)) {
  64. $output = multitosingle($value, $output);
  65. }
  66. else {
  67. $output[$key] = $value;
  68. }
  69. }
  70. return $output;
  71. }
  72.  
  73. /**
  74.  * function to get array/single nids which are attached with any of the taxonomy term of the given vocab.
  75.  * Returns Flat array of nids.
  76.  * @param
  77.  * $vocab_machine_name takes the machine name for the vocabulary.
  78.  * @return
  79.  * $nids_inside_vocab returns array of nids/single nid attached to any taxonomy term under the given vocab.
  80.  */
  81. function get_nodes_belong_to_vocab($vocab_machine_name) {
  82. $vocab_tids = get_taxonomy_term_ids_inside_vocab($vocab_machine_name);
  83. if (is_array($vocab_tids) && count($vocab_tids) > 0) {
  84. $nids_inside_vocab = get_nodes_directly_mapped_to_term($vocab_tids);
  85. return $nids_inside_vocab;
  86. }
  87. elseif (is_array($vocab_tids) && count($vocab_tids) == 0) {
  88. return array();
  89. }
  90. else {
  91. return "There is no vocab found with the given machine name";
  92. }
  93. }
  94.  
  95. /**
  96.  * function to get array/single nids inside a single taxonomy term/array of taxonomy terms
  97.  * function to get all node ids recursively inside a taxonomy term.
  98.  * if parent term is passed as parameter then the function returns nid belongs to the parent term and nids related to its child terms.
  99.  * Returns Flat array of nids.
  100.  * @param
  101.  * $tids takes array/single tids for which you need nids recursively.
  102.  * @return
  103.  * $nids flat array of nids
  104.  */
  105. function get_all_nodes_belong_to_taxonomy_hierarchy($tids) {
  106. if (is_array($tids)) {
  107. foreach ($tids as $key => $value) {
  108. if ($value <= 0) {
  109. return "Please Enter a valid taxonomy term id set";
  110. }
  111. }
  112. }
  113. if (is_array($tids) && !empty($tids)) {
  114. foreach ($tids as $key => $value) {
  115. $term = taxonomy_term_load($value);
  116. $hierarchical_terms_object[] = taxonomy_get_tree($term->vid, $term->tid);
  117. }
  118. $flat_terms_object = multitosingle($hierarchical_terms_object);
  119. if (is_array($flat_terms_object) && !empty($flat_terms_object)) {
  120. foreach ($flat_terms_object as $key => $value) {
  121. $flat_tids[] = $value->tid;
  122. }
  123. $flat_tids[] = $tids;
  124. return array_unique(get_nodes_directly_mapped_to_term($flat_tids));
  125. }
  126. else {
  127. $flat_tids = $tids;
  128. return array_unique(get_nodes_directly_mapped_to_term($flat_tids));
  129. }
  130. }
  131. elseif (!is_array($tids) && $tids > 0) {
  132. $flat_tids = $tids;
  133. get_all_nodes_belong_to_taxonomy_hierarchy(array($flat_tids));
  134. }
  135. else {
  136. return "Please Enter a valid term id";
  137. }
  138. }