0

I'm trying to change the post content of a page by hooking into the_post action, but the problem is that whatever I modify in there doesn't really get applied correctly.

The problem seems to come from the fact that query->setup_postdata creates a $pages variable and sets the content into it before calling the the_post hook. So whatever changes I do to the $post object doesn't reflect in the page.

Further down the road, it seems that the get_the_content function which calls the the_content filter uses the content from $pages instead of the $post->post_content. Thus if I change the content of the post, it will not output it correctly because WordPress bases itself off something else to print out the content.

Now if I hook unto the_content filter and replace the text using a simple hook mechanism such as:

public function wordpress_hooks_the_title($title){
    if(get_the_ID() == 4){ return 'test title'; }
    return $title;
}

public function wordpress_hooks_the_content($content){
    if(get_the_ID() == 4){ return 'test content'; }
    return $content;
}

It works fine for the content, but the title overrides many other titles in the page such as in the menu, even if I call get_the_ID() which is fine because the current $post is ok.

So I'm stuck trying to find the right way and not a half and half way. I'd like to use the_post only or the_content/the_title only but only half of them work fine in each context.

What would be the right approach?

  • Did you ever figure this out? I'm running into the same problem and it's about to drive me crazy. The thing is, I have this working on our live site (Manipulating $post->post_content directly), but it isn't working on our development site and I just can't figure out why. – Kevin Feb 16 '18 at 19:33
  • Sorry @Kevin but this was posted almost 6 years ago. I don't do wordpress anymore and i definitely don't remember the end to that story! – Mathieu Dumoulin Feb 19 '18 at 15:34
  • Hey it was worth a shot :) – Kevin Feb 19 '18 at 16:57
3

It seems you're actually looking for the the_content filter.

add_filter('the_content', 'modify_content');
function modify_content($content) {
    global $post;
    if ($post->ID != $desired_id)
        return $content;

    $modified_content = /* modify content here */;
    return $modified_content;
}
  • 1
    Thats what i'm already doing. – Mathieu Dumoulin Jul 27 '12 at 13:55
  • How can I modify the content like is loaded, not the_content (because it is the plain code), I need get the loaded content and do modify. – Milor123 Nov 11 '17 at 18:46
  • @MathieuDumoulin yout should always use the_content inside a loop. Try preceding the filter order putting it after any others. – vmassuchetto Nov 14 '17 at 10:15
  • @Milor123 then i think you should look for the $post->post_content variable in the loop, as this does not get formatting filters applied. – vmassuchetto Nov 14 '17 at 10:16

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.