What hook is executed just after wp_query has been executed? before template is rendered? I want to change the page title but it depends on what has been queried.

I have put it in template_redirect but not sure if it is the right hook


2 Answers 2


There are a few actions that can technically be used (including template_redirect), but a more appropriate action seems to be the_post; It passes the post object by reference making alterations easy:

function my_action( $post_obj ) {
    $post_obj->post_title = "New Title"

add_action( 'the_post', 'my_action' );

If you only need to change the title, you could also take a look at the the_title filter.

  • Your answer would have been very helpful had the OP asked about the post title. But they asked about the page title...
    – Lea Cohen
    Commented Aug 14, 2014 at 10:51
  • @LeaCohen When you are on a single page, the page title is the post title. A page is simply a built-in post type. the_post action is fired for pages and $post_obj will contain all the information for that page just like a post.
    – BigBagel
    Commented Aug 14, 2014 at 18:02
  • You're right, but who said they're on a single page?
    – Lea Cohen
    Commented Aug 16, 2014 at 18:34
  • @LeaCohen I used that as an example. It will work for any page data from any query. In the back-end, posts and pages are the exact same thing (post-types). After a query, a post object is created (whether it be a post, page, or custom post-type). the_post is the best action to use to alter the page's data because it exposes that object. Effectively, my answer is the same as Craig's; that the template_redirect action is one of several that technically can be used. I'm simply pointing out the most useful action for the OP's situation.
    – BigBagel
    Commented Aug 17, 2014 at 9:25
  • 1
    @LeaCohen Unless I am mistaken, the reason for your downvote was because you mistakenly thought the_post action only applied to posts. Now that you know better, do you have any other issues that need to be rectified before changing your vote? I assure you, I have used the the_post action as described; it works. This is a perfectly valid answer to the original question. Unless you have some actual experience or knowledge that would indicate that my answer is wrong (and by all means, tell me if you do so I can fix the answer), I fail to see any further issues.
    – BigBagel
    Commented Aug 19, 2014 at 7:42

Yep you're right template_redirect is fired right after wp which performs the query.

A very useful plugin you might want to look at is: https://wordpress.org/plugins/query-monitor/

This can help you to see what's loaded on a particular page during development, in addition to what hook is used

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.