I have a function that sets posts_per_page option depending on the category viewed.
I discovered and error which I can’t really understand: first this is how I get the error:
function custom_posts_per_page($query) {
if ( !is_admin() && is_category(4)) {
// Leaving this empty or with content
}
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );
So I get an error there if WP_Debug is turn to true and when I access a non-exixting category.
So if I enter http://localhost/zz/category/uncategorized
there is no problem but if I enter for example http://localhost/zz/category/aaaaaaaa
(Category aaaaaaaa doesn’t exist), It gets correctly to the 404 page but throws these errors:
Notice: Trying to get property of non-object in C:xampphtdocszzwp-includesquery.php on line 3420
Notice: Trying to get property of non-object in C:xampphtdocszzwp-includesquery.php on line 3422
Notice: Trying to get property of non-object in C:xampphtdocszzwp-includesquery.php on line 3424
What is wrong?
After a bit of investigation…
If you pass a category to
is_category
it usesget_queried_object
to grab data– see the source.get_queried_object
returnsNULL
for categories that do not exist. You can demonstrate that with:Load a valid category archive and then an invalid one.
The
is_category
method does not check to see whether it has a valid object before trying to use that object, hence the error. I would definitely consider that a bug.The only way I see around it is to check for the existence of the category first:
Or handle the logic yourself:
Barely tested. Possibly buggy. Caveat emptor. No refunds.