What you could do, is instead of using the pages()
method, use the toStructure()
method to get the contents of the field and then do whatever you like if the page does not exist:
<?php
$related = $page->related()->toStructure();
foreach($related as $r) {
$p = page($r->value());
// check if $p is a page object
if(is_a($p, 'Page')) {
echo $p->title();
} else {
// do whatever you want here, but do not use page methods
echo "The page does not exist";
}
}
?>