# Trouble filtering content output within a snippet

**URL:** <https://community.modx.com/t/trouble-filtering-content-output-within-a-snippet/4191>\
**Category:** General\
**Created:** [June 30, 2021, 3:12pm UTC](https://community.modx.com/t/trouble-filtering-content-output-within-a-snippet/4191 "2021-06-30T15:12:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffsydor](https://community.modx.com/user_avatar/community.modx.com/jeffsydor/32/363_2.png) [@jeffsydor](https://community.modx.com/u/jeffsydor)\
**Post date:** [June 30, 2021, 3:12pm UTC](https://community.modx.com/t/trouble-filtering-content-output-within-a-snippet/4191/1 "2021-06-30T15:12:19Z")

</div>

I’m trying to output content to a custom XML file and I need to filter the content one more time but I’m not sure how to do this.

I have an array of strings like this:  
`<practices>bf82cb61||f82e0c12||328d7ce8c</practices>`

Each of these tv selections corelate to a resource with the same **link\_attribute** and a snippet is in place to generate their names from the resource’s **pagetitle**.

```
<?php
$output = '';
$parts = explode("||", $input);
foreach($parts as $serviceId){
    $where = array(
        'link_attributes' => $serviceId,
        'template:IN' => [10,20]
    );
    $item = $modx->getObject('modResource', $where);
    if(($item)){
        $res = $item->toArray();
        $pagetitle = htmlentities($res['pagetitle']);
        $output .= '<serviceIndustry>' . $pagetitle . ' </serviceIndustry>';
    }
}
return $output;

```

I need to filter these results by an additional radio TV within each resource. Each resourse can be either a **service** , an **industry** or a **landing page**. I need to only include services & industries.

Any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![markh](https://community.modx.com/user_avatar/community.modx.com/markh/32/36_2.png) [@markh](https://community.modx.com/u/markh)\
**Post date:** [June 30, 2021, 3:26pm UTC](https://community.modx.com/t/trouble-filtering-content-output-within-a-snippet/4191/2 "2021-06-30T15:26:38Z")

</div>

```auto
    if ($item){
        $tv = $item->getTVValue('name_of_tv');
        if (!in_array($tv, ['service', 'industry'], true)) {
            continue; // this continues the foreach loop to the next serviceId
        }
        $res = $item->toArray();
        $pagetitle = htmlentities($res['pagetitle']);
        $output .= '<serviceIndustry>' . $pagetitle . ' </serviceIndustry>';
    }

```

If you’re running this code a lot (say \> 100 times) in an uncached call, you’ll likely want to optimise it as you’re otherwise going to send a lot of queries to the database. Perhaps caching a lookup array across requests (eg some map like `['bf82cb61' => 'Page Title if it's a service or industry']` you read into memory once per request) .

---

<div class="post-metadata">

**Author:** ![jeffsydor](https://community.modx.com/user_avatar/community.modx.com/jeffsydor/32/363_2.png) [@jeffsydor](https://community.modx.com/u/jeffsydor)\
**Post date:** [June 30, 2021, 3:35pm UTC](https://community.modx.com/t/trouble-filtering-content-output-within-a-snippet/4191/3 "2021-06-30T15:35:00Z")

</div>

ARHG!!! I just started writing and i was soo close! Thank you!!
