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 - I tried add_action and add_filter to 'the_post' or '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 - Simply adding 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).

tfl;dfr - 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 functions.php.

2 Answers 2


There is a the_post hook which should let you modify the post object:

function my_the_post_action( $post_object ) {
    // modify post object here
add_action( 'the_post', 'my_the_post_action' );

But the post data isn't where your thumbnails are stored. They are associated via a post meta field. That is what you are going to have to manipulate. Something like:

function my_thumbs($n, $object_id, $meta_key, $single ) {
  if ('_thumbnail_id' == $meta_key) {
    echo 'image link';
add_filter( "get_post_metadata", 'my_thumbs', 10, 4 );

Though I have no idea how complex that function would need to be.

Honestly, if you are running WP 2-something you need to update and spend the time converting your improvised system to Core functions. Sincerely.

  • I was trying the_post for the past couple of hours with no result. As always, after posting this I was able to do it. I have no idea what changed. I got the excerpt workin. Still - thank you for taking the time to answer. Now I'm still trying to work out the link to the feature image. More precise description of what needs to be done would be much appreciated. Also - could you explain the 10, 4 at the end of add_filter? I know it's some sort of priority but why those particular values? Also - I'm currently working with the latest wordpress version.
    – maltaannon
    Oct 18, 2015 at 22:39
  • This is all the description I can give you without knowing what/where/how you've hacked the old version of WordPress.
    – s_ha_dum
    Oct 18, 2015 at 22:42
  • Ok. Consider this simple situation. I have an image on my server. Say: someurl.com/image.jpg. None of my posts have the feature-image set. I want the someurl.com/image.jpg to be set as a feature-image for all my posts by using add_filter, so that the template "thinks" it was there to begin with - not in the database, but dynamically while the_loop or page or single is being executed.
    – maltaannon
    Oct 18, 2015 at 22:46

Thanks s_ha_dum for pointing me in the right direction. Here is the complete answer to the question I posted.

To inject excerpt into the post do this:

// function called by the_post filter (by reference) 
// needs no return statement. changes WP_Post object
function _inject_excerpt($post)
    // do whatever you like here. I needed to treat the content of the post
    // before the <!--more--> tag as excerpt

    // extract the text before the <!--more--> tag to be treated as excerpt
    $post->post_excerpt = preg_split('/<!--more(.*?)?-->/', $post->post_content)[0]; 
add_filter('the_post', '_inject_excerpt');

To inject a feature image to the post do this:

// function called by a post_thumbnail_html filter 
// injects post thumbnail / feature image into the post
// when there is none before processing so the template can see it
function _inject_thumbnail( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if (empty($html)) {
        $url = "preview.jpg"; // some processing might be needed to find the right url
        $html = "<img src='$url'>"; // some additional styling might be needed
    return $html;
add_filter( 'post_thumbnail_html', '_inject_thumbnail');

Hope this helps someone else in need.

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.