# Get all childs of parent with specific template and set tv value

**URL:** <https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499>\
**Category:** General\
**Created:** [January 16, 2021, 11:39am UTC](https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499 "2021-01-16T11:39:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxojeck](https://community.modx.com/letter_avatar_proxy/v4/letter/o/e274bd/32.png) [@oxojeck](https://community.modx.com/u/oxojeck)\
**Post date:** [January 16, 2021, 11:39am UTC](https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499/1 "2021-01-16T11:39:42Z")

</div>

Hi everyone! How to get all childs of parent with specific template and set tv value through the console? Here is my beginning code:

```
<?php
if(!$docs = $modx->getCollection('modResource', array(
    'template' => 11
))){return;}
foreach($docs as $doc){
    $doc->setTVValue(2,'dir');
    $doc->save();
}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 16, 2021, 12:55pm UTC](https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499/2 "2021-01-16T12:55:20Z")

</div>

if you only want the direct children you can set the `parent` in your query.

```auto
$parent_id = 10;
if(!$docs = $modx->getCollection('modResource', array(
    'template' => 11, 'parent' => $parent_id
))){return;}

```

If you want all the children in the hierarchy you could use the function [getChildIds()](https://docs.modx.com/current/en/extending-modx/modx-class/reference/modx.getchildids):

```auto
$parent_id = 10;
$children = $modx->getChildIds($parent_id);
if(!$docs = $modx->getCollection('modResource', array(
    'template' => 11, 'id:IN' => $children
))){return;}

```

---

<div class="post-metadata">

**Author:** ![oxojeck](https://community.modx.com/letter_avatar_proxy/v4/letter/o/e274bd/32.png) [@oxojeck](https://community.modx.com/u/oxojeck)\
**Post date:** [January 17, 2021, 4:47am UTC](https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499/3 "2021-01-17T04:47:54Z")

</div>

The first solution is fine, but I have many parents. I need to change the tv each child of each parent with template 11. I suppose I need to use two foreach loops.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 17, 2021, 10:59am UTC](https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499/4 "2021-01-17T10:59:31Z")

</div>

You can do this with 2 foreach loops or with a query that uses a join:

```auto
$q = $modx->newQuery('modResource');
$q->innerJoin('modResource','Parent');
$q->where(array(
    'template' => 11, //template of child resource
    'Parent.template' => 11 //template of parent resource
));
if(!$docs = $modx->getCollection('modResource', $q)){return;}

```

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [January 21, 2021, 8:55am UTC](https://community.modx.com/t/get-all-childs-of-parent-with-specific-template-and-set-tv-value/3499/5 "2021-01-21T08:55:08Z")

</div>

Closed on January 21, 2021, 8:55am UTC

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.
