My goal here is to create a mini-plugin that allows me to create social share links, as such, I am very, very interested in getting the most up-to-date version of a queried object's link/title.

I've discovered through brute-forcing that the earliest a WP_Post-like (or queried object, page, etc.) is available to us is parse_query, thing is, this makes me think. A lot of popular plugins/themes are still filtering titles and so on inside hooks like pre_get_posts and so on.

When exactly can I hook so that I get the final version of a post while still allowing others to interact with my API?

It strikes me that loop_end is the go-to, after seeing how the core operates, but if I fire my functionality this late, others won't be able to hook into it earlier. I feel like I'm architecturing the wrong way.


I think your conception of how hooks work is slightly off. If you are outputting something that contains the URL and title of the queried object, then this isn't something you'd typically need to worry about.

You bring up two types of filters that you're concerned about: using pre_get_posts, and filtering titles.

In the case of pre_get_posts, this does not modify the queried object. This hook is used to modify what query is performed based on the queried object. The queried object itself does not change. For example if the user visits the URL to category "Foo", but a plugin uses pre_get_posts to change the query so that this category returns posts that belong to category "Bar", the queried object is still "Foo", and other elements of the page, such as the page's <title> tag will still use "Foo". In this situation you would still want the details of "Foo", to match the title tag, so the queried object is what you'd use.

So you don't need to worry about pre_get_posts. What about other filters that could affect the queried object? The important thing to understand is that these filters do not modify the state of the queried object.

For example, let's say the queried object is a WP_Post object, with the title "Foo". Even if a plugin is running that uses the the_title filter to modify all post titles to be "Bar", the post_title property of WP_Post will still be "Foo". This is because these filters do not modify the state of the object. Instead, The way these filters work is that they are applied on output. In the case of post titles this is done via the get_the_title() function, which runs the post_title property through any registered filters each time it's used.

So if you wanted to get the title of the queried object for a page, and respect any filters that have been added by other plugins, this would be incorrect:

$title = get_queried_object()->post_title;

Instead use the correct function, which will apply 3rd-party filters to your value:

$title = get_the_title( get_queried_object() );

Or manually apply the filters yourself:

$title = apply_filters( get_queried_object()->post_title, get_queried_object_id() );

So basically this boils down to: Don't worry about the "state" of the queried object, or what the latest hook is to do this or that. Instead just make sure you're using the correct functions and APIs to get the data you need, and your plugin will also reflect any changes made by other plugins.

| improve this answer | |
  • I understand how hooks work. My concern was exactly what you just posted. Is there something going on inside pre_get_posts that I should know about? In my case, however, I am very, very interested in the output part of things. Then you went ahead and explained it to me that get_the_title does all the heavy-lifting for me. That's exactly what I was looking for. I wanted to get the very latest version of whatever filterable field I needed. It's often times that inside a hook, there's a lot more happening that I can't directly see that actually impacts output. – Daniel M Jan 23 at 9:32
  • Also, you are an amazing teacher and your work is invaluable. I think you might get errked by the fact that I'm asking these questions, but I really, really want to understand, as deeply as I can, how the Core works so that I can develop good software. – Daniel M Jan 23 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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