Skip to content

Instantly share code, notes, and snippets.

@bajanReece
Created March 19, 2013 11:37
Show Gist options
  • Save bajanReece/5195434 to your computer and use it in GitHub Desktop.
Save bajanReece/5195434 to your computer and use it in GitHub Desktop.
Getting Taxonomy content from a Drupal node.
<?php
function get_taxonomy_content($tid, $limit = 10) {
/**
A note on variables:
field_data_field_article_rating --- this is the name of taxonomy table you're referring to
field_article_rating_tid --- this is the name of the term ID column in the table above
**/
$out = array();
$query = db_select('node', 'n');
$query->join('field_data_field_article_rating', 'c', 'n.nid = c.entity_id');
$query->condition('n.status', 1, '=')
->condition('c.field_article_rating_tid', $tid)
->fields('n', array('nid', 'title'))
->orderBy('n.type', 'ASC');
$query = $query->extend('PagerDefault')->limit($limit);
$result = $query->execute();
foreach($result as $row){
// first we get the node content
$nodeObj = node_view(node_load($row->nid),'teaser');
// then we get the node URL
$options = array('absolute' => TRUE);
$url = url('node/' . $row->nid, $options);
// then we output an array
$out[$row->nid] = array('nid'=>$row->nid, 'title'=>$row->title, 'obj'=>$nodeObj, 'url'=>$url);
} // end foreach
return $out;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment