<p>How do I get all of an entry's children using the Element API?</p>

<p>I want to return something like:</p>

<pre><code>data: [
    title: 'Root Entry'
    uri: 'root-entry'
    children: [
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-1'
            children: [
                {
                    title: 'Level 3 Entry'
                    uri: 'level-3-entry'
                    children: [...]
                },
            ]
        },
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-2'
            children: [...]
        },
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-3'
            children: [...]
        },
    ]
]
</code></pre>

<p>I would want to get all entries up to the depth level of 4. Some entries wont have children past level 3 and some wont have any past level 2. I have tried doing a for loop traversal but cannot figure out why <code>$entry-&gt;children</code> returns everything but children entries.</p>

CC BY-SA 4.0
2019-12-03T01:13:58.987Z
Perhaps an obvious question, but have you tried the standard **{% nav entry in entries %}** loop? That should get all children and children's children.
4midori
CC BY-SA 4.0
2019-12-03T17:22:08.963Z
Sorry I forgot to mention that I was using the Element API plugin for getting the entries
kayq
CC BY-SA 4.0
2019-12-07T21:50:24.613Z
[This post](https://craftcms.stackexchange.com/a/32751/3630) should help achieve what you need.
Oli
<entries><plugin-elementapi>
How to get an Entry in structure and all of its children and it's children's children with Element APi?
# How to get an Entry in structure and all of its children and it's children's children with Element APi?

(Asked by: username_0 on 2019-12-03)

How do I get all of an entry's children using the Element API?
I want to return something like:
data: [
    title: 'Root Entry'
    uri: 'root-entry'
    children: [
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-1'
            children: [
                {
                    title: 'Level 3 Entry'
                    uri: 'level-3-entry'
                    children: [...]
                },
            ]
        },
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-2'
            children: [...]
        },
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-3'
            children: [...]
        },
    ]
]

I would want to get all entries up to the depth level of 4. Some entries wont have children past level 3 and some wont have any past level 2. I have tried doing a for loop traversal but cannot figure out why $entry->children returns everything but children entries.

**Tags:** entries, plugin-elementapi
> **Comments:**
> **username_1**: Perhaps an obvious question, but have you tried the standard **{% nav entry in entries %}** loop? That should get all children and children's children.  
> **username_0**: Sorry I forgot to mention that I was using the Element API plugin for getting the entries  
> **username_2**: [This post](https://craftcms.stackexchange.com/a/32751/3630) should help achieve what you need.  

---

## 0 answers

