I am looking for a way to modify
WP_Post object before it's processed and displayed on the page.
Reason - I built my site when WP was still in version 2.0, or even earlier. I didn't attend to it for a couple of years, but now I'm back making updates. First version of my site was modded with
my-hacks.php and some clever uses of
add_filter('the_content', some_function); Good thing is they still work, but I'm having trouble adding some functionality to it.
tl;dr - My posts do not have a "Featured Image" (or excerpt), but due to the way I built the site, I know where the images are and I can generate a URL for each post dynamically (same goes for the excerpt); I do not want to add them to the post. I want them to be added to the results of the query before it's displayed and processed by the Theme, so that all the goodies the theme provides "think" the data is actually there.
What I already did -
'the_content', and I've tried playing with
add_post_meta (which seems to be a good way to go), but I can't make it work.
What I think is a good way to go -
add_filter('the_content') won't do, since most templates these days check for the meta data in the post, like the thumbnail/featured image or the excerpt, so adding some content to the post won't cut it. That's why I think it would be best to process all the post data before it's being handled by the template. This seems to be a good place to use hooks, but which one, and how do I use it?
What I'd appreciate -
A simple example that would work in a loop and in a singular post, and change it's title by modifying
the_post data instead of using
the_title(). I would be most grateful for an example where a featured image is being placed into the post where there is none (can be a static URL) and excerpt as well (can be static text).
To clarify -
I do not want this to make changes in the database, just the results the query returned so that it seems it's been in the database from the beginning. This all should be achieved via
functions.php, or some other wrapper (adding
require_once("my_functions.php") to the
functions.php of the theme).
How to change the
WP_Post object (it's content, meta, thumbnail, custom fields...), in and outside the loop by modifying the results of the query, so that the template/theme "thinks" the changes were already there and it can process them as intended? Should use an external file (not changing the template itself) like