“Trying to get property of non-object” in pre_get_posts
-
I’m doing something like this in functions.php
function modify_loop($query) { if(!is_admin() && $query->is_main_query()) { if($query->is_category(array(7,8,18,23,24)) { // stuff } } } add_action('pre_get_posts', 'modify_loop');
If I load a non-existant URL on my site like https://example.com/this-page-doesnt-exist it will show the 404 page but with the PHP notice:
Trying to get property ‘term_id’ of non-object in ~/wp-includes/class-wp-query.php
Apparently it has something to do with the check for category IDs; without the array in is_category() the error goes away.
Doing avar_dump($query->is_category()); var_dump(is_404());
inside the function it shows true for is_category() and false for is_404(). Why is that?
I have to add that I have Yoast SEO set to remove the category base from URLs. Perhaps this has something to do with it?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘“Trying to get property of non-object” in pre_get_posts’ is closed to new replies.