5

When a page request is made, at what point does is_front_page start working? As I recall, if you try using it too early it will always return false.

My assumption is that the wp action is the first point that is_front_page will work, however, the conditional check for is_front_page is part of the WP_Query class, so wouldn't parse_query then be the earliest safe use?

1 Answer 1

7

The parse_query() method of the WP_Query class sets the variables on which the conditional tags are based.
The parse_query hook are executed before returning from parse_query() method, variables are already set (eg. is_home, used by is_front_page()) and is_front_page() will work, but function that changes the current state can be hooked to parse_query (it's still parse_query() method, which sets variables like is_home).

Hooks pre_get_posts is executed directly after parse_query, that is why it is for me the first safe hook to use conditional tags.

Conditional Tags in Codex

You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

However: if you have a reference to the query object (for example, from within the parse_query or pre_get_posts hooks), you can use the WP_Query conditional methods (eg: $query->is_search())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.