After a bit of investigation…
If you pass a category to is_category
it uses get_queried_object
to grab data– see the source. get_queried_object
returns NULL
for categories that do not exist. You can demonstrate that with:
function custom_posts_per_page($query) {
var_dump(get_queried_object());
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );
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:
function custom_posts_per_page($query) {
if ( !is_admin() && term_exists(4,'category') && $query->is_category(4)) {
// Leaving this empty or with content
}
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );
Or handle the logic yourself:
function custom_posts_per_page($query) {
if ( !is_admin() && $query->is_category()) {
$qobj = get_queried_object();
if (!empty($qobj)
&& (isset($qobj->taxonomy) && 'category' == $qobj->taxonomy && 4 == $qobj->term_id)
) {
var_dump($qobj);
}
// Leaving this empty or with content
}
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );
Barely tested. Possibly buggy. Caveat emptor. No refunds.