Skip to content

Commit

Permalink
Post Content/Title: Reflect changes when previewing post (#37622)
Browse files Browse the repository at this point in the history
When inside the main loop, we want to use a queried object to apply `the_preview` for the current post.
We force this behavior by omitting the post ID argument from the `get_the_content` and `get_the_title.`

Co-authored-by: Adam Silverstein <[email protected]>
  • Loading branch information
Mamaduka and adamsilverstein committed Dec 28, 2021
1 parent 02aa04e commit 884341d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ function render_block_core_post_content( $attributes, $content, $block ) {

$seen_ids[ $post_id ] = true;

// Check is needed for backward compatibility with third-party plugins
// that might rely on the `in_the_loop` check; calling `the_post` sets it to true.
if ( ! in_the_loop() && have_posts() ) {
the_post();
}

$content = get_the_content( null, false, $post_id );
// When inside the main loop, we want to use queried object
// so that `the_preview` for the current post can apply.
// We force this behavior by omitting the third argument (post ID) from the `get_the_content`.
$content = get_the_content( null, false );
/** This filter is documented in wp-includes/post-template.php */
$content = apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) );
unset( $seen_ids[ $post_id ] );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function render_block_core_post_title( $attributes, $content, $block ) {
}

$post_ID = $block->context['postId'];
$title = get_the_title( $post_ID );
$title = get_the_title();

if ( ! $title ) {
return '';
Expand Down

0 comments on commit 884341d

Please sign in to comment.