1

how do I get taxonomy based node content through custom query? I have taxonomy terms displayed using drupal api function. Now I need to fetch content based on terms id or name from database? Can any one please help me.Either using entity field query or SQL?

$tree=taxonomy_get_children(1);
 foreach ($tree as $term)
 {
 $item[] = $term->name;
 $child_term = taxonomy_get_children($term->tid);
 if (!empty($child_term))
 {
 <a href="?name=<?php print $term->tid; ?>">
 <?php
 print $term->name."<br>";
 ?>
 </a>
 foreach ($child_term as $child)
 {
 $item[] = $child->name;
 <a href="?tid=<?php print $child->tid; ?>" name="tid"> 
 <?php
 print $child->name."<br>";
 ?>
 </a>
 <?php
 $nodes = taxonomy_select_nodes($child->tid);
 $node = node_load_multiple($nodes);
 $nidd=(int)$node->nid;
 foreach($node as $node1):
 $tids = field_get_items('node', $node1, 'field_product_sub_cat');
 foreach ($tids as $tid){
 if($tid['tid'] == $child->tid){
 print $node1->title;
 print render(field_view_field('node', $node1, 'body')); 
 }
 }
 endforeach;
 else
 print $term->name."<br>";

This is the snapshot of content type

1

You can get nodes as shown below:

$nodes = taxonomy_select_nodes($tid); // $tid is term id
$nodes = node_load_multiple($nodes);

Ref: taxonomy_select_nodes -> https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_select_nodes/7

Update #1

To get some fields out of $node object use following:

foreach($nodes as $node):
    $tids = field_get_items('node', $node, '<TERM_FIELD_NAME>');
    foreach ($tids as $tid){
        if($tid['tid'] == <SELECTED_TID>){
            print $node->title;
            print render(field_view_field('node', $node, 'body')); 
            print render(field_view_field('node', $node, 'tags')); 
        }
    }
endforeach;

To render complete node you can use node_view function. In order to render multiple nodes you can use node_view_multiple function as shown below:

$content = node_view_multiple($nodes, 'full');
print render($content);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.