I know this isn’t exactly the same issue, however the response below is maybe a clue. The person says that the OP’s query might not have returned a WP_Post object. So even though your filter callback gets called at the correct times, maybe $wp_query contains a query that didn’t return any results OR it doesn’t contain the WP_Post object as we’ve already said.
$wp_query->queried_object->ID has several potential points of failure:
Your query may not be dealing with a WP_Post object. The queried_object can also reference a taxonomy in some circumstances.
Your query may not have actually identified any results. Your queried_object could be null, and thus you’re asking for a property on an nonexistent object.
You could be using $wp_query in code that does not have a reference to that global. If you determine that $wp_query itself is null, you should add
global $wp_query;
… somewhere above your code.
========================
p.s. if I try and italicise a variable like $with_underscore in these posts I get $with_underscore and not $nounderscore.
