1

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.

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

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

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

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

?>
0

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->vid);

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

  • Thanks for quick replay..i have used above line of code get all parent with child terms but how can i integrate this to with my bundle based query please suugest. – Parvateesam Apr 1 '16 at 10:21
  • Please see above code how can i use $sub in entity field condition if i use that correct way then automatically display parent with child terms based on admin side selection currently displaying formate like what given in vocabulary i want to map this to my content type please advice. – Parvateesam Apr 1 '16 at 10:32

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.