Check if blog posts exist

Underneath project pages I would like to display related news items. I would only like to show this snippet if related news items actually exist. So I am filtering for them like this:

<?php $posts = page('home')->children()->visible()->filterBy('project', $pageProject)->flip()->limit(4);
if (!empty($posts)): ?>

After this, I create the html for showing these items.

However, the second line in my code doesn’t seem to work, as it always shows the related news bit. It works fine for project which have related news items, by the way.

This is the full snippet.

How do I properly check if the news items exist?

<?php $posts = page('home')->children()->visible()->filterBy('project', $pageProject)->flip()->limit(4);
if ( $posts->isNotEmpty() ): ?>

PHP’s empty() checks if a variable is empty, but in case of a collection without elements, the variable is not empty, because it contains the object.

Thank you, this works!