How can i display child terms under parent terms and assigned articles using entity field query? Hi i have one content type with  two vocabulary term references one is called category and terms is A,B,C like the 2nd one resources it contains parent with child terms like below hierarchy 

Resource 1 

    Subsection 1 
    Subsection 2
    Subsection 3
    Subsection 4

Resource 2
Resource 3
Resource 4   


so i want to display articles based on first taxonomy  category then 2nd taxonomy resources related items using entity field query below hierarchy 

Resource 1 

    Subsection 1 

        Article 1
        Article 2

    Subsection 2
    Subsection 3
    Subsection 4

Resource 2
Resource 3
Resource 4


below is my code for display only 1st taxonomy based articles fetching but i need to display both cases please help.

        &lt;?php
        $vocab = taxonomy_vocabulary_machine_name_load('resource_category');
        $trees = taxonomy_get_tree($vocab-&gt;vid);
        $array_tree = array();
        $array_tree1 = array();
        foreach($trees as $tree)
        {
            if($tree-&gt;depth == 0)
            array_push($array_tree,$tree-&gt;tid);
        }
        $i = 0;

        foreach($trees as $tree)
        {
            foreach($tree-&gt;parents as $parent)
            {
                if($parent != 0)
                {
                    $array_tree1[$parent][$i]=$tree-&gt;tid;
                    $i++;

                }
                //~ if(array_search($tree-&gt;tid,$array_tree))
                //~ array_push($array_tree1,$tree-&gt;tid);
            }

        }
?&gt;
&lt;ul&gt;
&lt;?php 
foreach($trees as $tree)
{
    if($tree-&gt;depth == 0) {
    echo '&lt;li&gt;'.$tree-&gt;name;
    if(count($array_tree1[$tree-&gt;tid]))
    echo '&lt;ul&gt;';
            foreach($array_tree1[$tree-&gt;tid] as $sub)
            {
                echo '&lt;li&gt;'.$sub.'&lt;/li&gt;';
                $query_resources = new EntityFieldQuery();
                $query_resources-&gt;entityCondition('entity_type', 'node')
                        -&gt;entityCondition('bundle', 'resouces')
                        -&gt;propertyCondition('status', NODE_PUBLISHED)
                        -&gt;fieldCondition('field_resource_category', 'tid', '21')
                        -&gt;fieldCondition('field_resources_subcategory');
                $result_resources = $query_resources-&gt;execute();
                if (isset($result_resources['node'])) {
                    $news_items_resources = array_keys($result_resources['node']);
                    $news_items_res = entity_load('node', $news_items_resources);
                        //echo '&lt;pre&gt;';print_r($news_items_res); echo '&lt;/pre&gt;';     
                } else {
                    echo '&lt;p&gt;&lt;strong&gt;No Resources&lt;/strong&gt;&lt;/p&gt;';
                }
                    }
            echo '&lt;/ul&gt;';
            echo '&lt;/li&gt;';
        }
}

?&gt;
Term relationships aren't kept in a table that the entity API knows about, so you can't use an EFQ to query them.

taxonomy_get_tree() exists for exactly this purpose:


  Create a hierarchical representation of a vocabulary.


e.g.

$vocab = taxonomy_vocabulary_machine_name_load('foo_vocab');
$tree = taxonomy_get_tree($vocab-&gt;vid);


You can optionally provide a $parent_id as a 2nd argument, to force the root of the tree to a specific term.