WordPress.org

Make WordPress Core

Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#44788 closed enhancement (invalid)

the_content() strange behavior - we can't change global "post_content" from "$post->post_content"

Reported by: Tkama Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.8
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

Look at this code, is it normal?

<?php
global $post;

echo $post->post_content; //> Some text

$post->post_content = 'New text';

the_content(); //> Some text

Change History (5)

#1 @windyjonas
13 months ago

Yes, the_content starts with a

$post = get_post();

#2 @subrataemfluence
13 months ago

Just to elaborate @windyjonas reply a bit:

the_content() calls the following function in wp-includes/post-template.php

function the_content( ... ) {
  $content = get_the_content( ... ) // invokes get_the_content()
  ...
}

function get_the_content( ... ) {
  ...
  $post = get_post();
}

#3 @SergeyBiryukov
13 months ago

  • Summary changed from the_content() strange behavior - we cant change golobal "post_content" from "$post->post_content" to the_content() strange behavior - we can't change global "post_content" from "$post->post_content"

#4 @SergeyBiryukov
13 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Type changed from feature request to enhancement

To clarify a bit more, setting $post->post_content does affect the $post global, however that's not the value used in get_the_content(), at least not directly.

The function takes $content from the $pages global, which is set up in WP_Query::setup_postdata earlier.

It's not quite clear when exactly your snippet runs, but it should work if you add setup_postdata( $post ) after changing $post->post_content.

#5 @Tkama
13 months ago

I need to add some changes to post_content before using the_content(). But it was not so easy. I made it through global $pages. But solution to do it with setup_postdata( $post ) seems more correct, thanks for it.

Note: See TracTickets for help on using tickets.