Adding this to the top of your
filterPostTitle() function also fixes the issue.
if ( is_404() ) return $title;
wp_query->post is not being set when a 404 occurs (we knew this anyway but this is definitely confirmed by that line of code). Is it normal for wp_query->post to not be set during a 404?
I have a custom 404 filter set up in my child theme which I have disabled, however the issue still occurs (I went back to Twenty Fifteen previously anyway so my 404 could not have been called in that case).