is_category in pre_get_posts results in php notices

is_category('category-name') won’t work until after the query is run. If we check source code where those notices are generated, we can see that it’s using get_queried_object, which gets populated with the results of the main query, and at the pre_get_posts stage will still be empty.

As an alternate, check the contents of $query->query_vars, which will be an array of query vars parsed from the requested URL.

Leave a Comment