I needed to add an extra condition here:

`if($pageVersion && !$pageVersion->isApproved()) {`