Return projects that contain specific Image

Hello everybody,

I am trying to cycle through all my »projects« and for those projects that have an Image with a name that contains poster I want to do something. And for those that don’t have such an image I want to do something else.
This is what I came up with by browsing the forum (I have no understanding of php).

But for all projects the function returns Y even though some of the pages don’t contain a file with the filename containing poster.

What am I doing wrong? Any suggestions?

<?php foreach(page('projects')->children()->visible() as $project): ?>
<?php if ($image = $project->images()->filterBy('filename', '*=', 'poster')): ?>
<p>YES</p>
<?php else: ?>
<p>NO</p>
<?php endif ?>
<?php endforeach ?>

Try this

<?php foreach(page('projects')->children()->visible() as $project): ?>
    <?php if ($project->images()->filterBy('filename', '*=', 'poster')->count() > 0): ?>
      <p>YES</p>
    <?php else: ?>
      <p>NO</p>
    <?php endif ?>
<?php endforeach ?>
1 Like

That did the job. Much appreciated. Thank you!