diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index c3910d2b3a2d3..e466d6e9739bf 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2523,6 +2523,12 @@ function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { * An empty string if a valid but non-existing post ID is passed. */ function get_post_meta( $post_id, $key = '', $single = false ) { + // Make sure meta is get for the post, not for a revision. + $the_post = wp_is_post_revision( $post_id ); + if ( $the_post ) { + $post_id = $the_post; + } + return get_metadata( 'post', $post_id, $key, $single ); } diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 548c5eb2cabf8..45e1de66f0224 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -756,4 +756,23 @@ public function test_use_block_editor_for_post() { $this->assertTrue( use_block_editor_for_post( $restless_post_id ) ); remove_filter( 'use_block_editor_for_post', '__return_true' ); } + + /** + * Ensure get_post_meta does not try to get metadata from a revision + * + * @ticket 58763 + * @covers ::get_post_meta + */ + public function test_get_post_meta() { + $post_id = self::factory()->post->create(); + $revision_id = self::factory()->post->create( + array( + 'post_type' => 'revision', + 'post_parent' => $post_id, + ) + ); + + update_post_meta( $revision_id, 'test', 'testvalue' ); + $this->assertSame( 'testvalue', get_post_meta( $revision_id, 'test', true ) ); + } }