Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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;
share|improve this answer
Thats what i'm already doing. – Mathieu Dumoulin Jul 27 '12 at 13:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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