# Filter to exclude files that contain specific string in their name

**URL:** <https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 23, 2015, 1:54pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573 "2015-03-23T13:54:52Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 23, 2015, 1:54pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/1 "2015-03-23T13:54:52Z")

</div>

I can fetch an image that contains e.g. the string “cover” in its filename like so:

```
$page->images()->filterBy('filename','*=','cover')->first()

```

Is there a way to turn this method around, so I get a collection of all the images in a folder, _except_ the one that contains the string “cover” in its name? I know you can [define your own filter methods](http://getkirby.com/docs/advanced/filter-methods#define-your-own-method), but how exactly would I go about that? I’d be grateful for any hints.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [March 23, 2015, 2:08pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/2 "2015-03-23T14:08:17Z")

</div>

A lil messy, but maybe this could work (if it’s always only one ‘cover’)?

```php
$page->images()->filterBy('filename', '!=', $page->images()->filterBy('filename','*=','cover')->first()->filename());

```

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 23, 2015, 2:15pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/3 "2015-03-23T14:15:45Z")

</div>

Ha! I _knew_ I was thinking overly complicated 😄 That worked perfectly fine, thanks for the tip!

---

<div class="post-metadata">

**Author:** ![bastianallgeier](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bastianallgeier/32/7569_2.png) [@bastianallgeier](https://forum.getkirby.com/u/bastianallgeier)\
**Post date:** [March 23, 2015, 2:16pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/4 "2015-03-23T14:16:31Z")

</div>

I’d rather create my own filter here:

```php
$page->images()->filter(function($image) {
  return !str::contains($image->filename(), 'cover');
});

```

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 23, 2015, 2:18pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/5 "2015-03-23T14:18:42Z")

</div>

Solid! I’ll implement that. Thanks a lot you guys!

---

<div class="post-metadata">

**Author:** ![snazz](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@snazz](https://forum.getkirby.com/u/snazz)\
**Post date:** [March 24, 2015, 10:56am UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/6 "2015-03-24T10:56:28Z")

</div>

Hello Guys,

I was searching for this Solution to exclude images on Page.

Because every uploaded image is shown in the Slider.

My Snippet for the files is

```
<?php foreach($page->images() as $image): ?>
 <img class="rsImg" src="<?php echo $image->url() ?>" alt="">
<?php endforeach ?>

```

Where do I have to put that snippet above to get this Filter on one page to work ☹ I’ve tried several possibilities.

Hope you can help me.

Have a nice day 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [March 24, 2015, 11:06am UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/7 "2015-03-24T11:06:01Z")

</div>

What kind of images are you trying to filter?

In your example it’d be

```php
<?php
$images = $page->images()->filter(function($image) {
  return !str::contains($image->filename(), 'cover'); // whatever you want to remove
});
?>
<?php foreach($images as $image): ?>
 <img class="rsImg" src="<?php echo $image->url() ?>" alt="">
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![snazz](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@snazz](https://forum.getkirby.com/u/snazz)\
**Post date:** [March 24, 2015, 11:17am UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/8 "2015-03-24T11:17:18Z")

</div>

Wow, thank you for the fast response. It worked like a charm 🙂

Thank you very much.

---

<div class="post-metadata">

**Author:** ![tobiasweh](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobiasweh/32/13376_2.png) [@tobiasweh](https://forum.getkirby.com/u/tobiasweh)\
**Post date:** [August 3, 2015, 11:46am UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/9 "2015-08-03T11:46:51Z")

</div>

I made a custom filter out of @distantnative’s code, that can be used with `\filterBy`, like this

```php
$page->images()->filterBy('filename', '!*=', '-big')

```

where `!*=` means “doesn’t contain”. To install this filter create a file `filterStringContainsNot.php` (or whatever you like) at `plugins/` and save this code:

```php
<?php

collection::$filters['!*='] = function($collection, $field, $value, $split = false) {

  foreach($collection->data as $key => $item) {
    if(!str::contains((string)collection::extractValue($item, $field), $value)) continue;
    unset($collection->$key);
  }

  return $collection;

};

```

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [January 19, 2016, 5:07pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/10 "2016-01-19T17:07:50Z")

</div>

Thanks for your filter!  
It is just what I was looking for 🙂

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [March 14, 2018, 8:17pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/11 "2018-03-14T20:17:20Z")

</div>

Hey guys. I’m looking to filter Kirby users based on a custom field named “position”. The following will allow me to filter based on a single value.

```
$members = $site->users()->filterBy('role', 'member')->filter(function($member){
    return str::contains($member->position(), 'Officer');
});

```

This is great. It allows me to build a collection of members that have “Officer” in the position field, but can this be tweaked to allow me to filter multiple position values. For example what if I wanted to get a collection of members that have either “Officer” or “Executive Board” as a value in the position field?

Any insight would be awesome.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 14, 2018, 8:41pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/12 "2018-03-14T20:41:45Z")

</div>

You can either use and or condition or check if the value is in an array:

```auto
$positions = ['officer', 'whatever'];
    return in_array($member->position(), $positions);

```

---

<div class="post-metadata">

**Author:** ![elaltito](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/elaltito/32/7940_2.png) [@elaltito](https://forum.getkirby.com/u/elaltito)\
**Post date:** [January 24, 2020, 4:29pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/13 "2020-01-24T16:29:32Z")

</div>

I am trying to achieve the same result, i.e show all images except for images with the word ‘cover’ in the name. For some reason this code is still showing me all the images, including those with ‘cover’ in the file name.

```
<div class="photo-grid">
  <?php foreach($page->images()->filterBy('filename', '!=', 'cover') as $image): ?>
      <img src="<?php echo $image->url() ?>" alt="">
  <?php endforeach ?>
</div>

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 24, 2020, 4:37pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/14 "2020-01-24T16:37:13Z")

</div>

@elaltito Do you want to exclude all files that contain the string `cover` or only the one with the filename `cover`. For the first option, use the code provided by @bastianallgeier

```auto
$images = $page->images()->filter(function($image) {
  return !str::contains($image->filename(), 'cover');
});

foreach ($images as $image) {
  echo $image->url();
}

```

Are you on Kirby 2 or 3? On Kirby 3, there is a shorter way to achieve this.

---

<div class="post-metadata">

**Author:** ![elaltito](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/elaltito/32/7940_2.png) [@elaltito](https://forum.getkirby.com/u/elaltito)\
**Post date:** [May 5, 2020, 4:46pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/15 "2020-05-05T16:46:29Z")

</div>

Thanks for your help @texnixe ! 🙂 I have just finished upgrading to Kirby 3, what would be the shorter way to achieve this then? And yes I would like to exclude all files that include the string ‘cover’, for example “photo\_cover.jpg”

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 5, 2020, 4:50pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/16 "2020-05-05T16:50:25Z")

</div>

@thewebprojects: [https://getkirby.com/docs/reference/objects/files/filter-by#available-filter-methods](https://getkirby.com/docs/reference/objects/files/filter-by#available-filter-methods)

---

<div class="post-metadata">

**Author:** ![elaltito](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/elaltito/32/7940_2.png) [@elaltito](https://forum.getkirby.com/u/elaltito)\
**Post date:** [May 5, 2020, 4:58pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/17 "2020-05-05T16:58:17Z")

</div>

Great, worked perfectly! Thanks again for your amazingly quick response! 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:23pm UTC](https://forum.getkirby.com/t/filter-to-exclude-files-that-contain-specific-string-in-their-name/573/18 "2025-02-06T17:23:01Z")

</div>


