diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index a34b34b6ceb5f..34bfd267a3fe3 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -2511,7 +2511,8 @@ function get_next_posts_page_link( $max_page = 0 ) { * @return string|void The link URL for next posts page if `$display = false`. */ function next_posts( $max_page = 0, $display = true ) { - $output = esc_url( get_next_posts_page_link( $max_page ) ); + $link = get_next_posts_page_link( $max_page ); + $output = $link ? esc_url( $link ) : ''; if ( $display ) { echo $output; diff --git a/tests/phpunit/tests/link/nextPosts.php b/tests/phpunit/tests/link/nextPosts.php new file mode 100644 index 0000000000000..de43f5fedda9f --- /dev/null +++ b/tests/phpunit/tests/link/nextPosts.php @@ -0,0 +1,41 @@ +post->create_many( 3 ); + $paged = 2; + $wp_query = new WP_Query( + array( + 'post_type' => 'post', + 'posts_per_page' => 1, + 'paged' => $paged, + ) + ); + } + + /** + * The absence of a deprecation notice on PHP 8.1+ also shows that the issue is resolved. + * + * @ticket 59154 + */ + public function test_should_return_empty_string_when_no_next_posts_page_link() { + $this->assertSame( '', next_posts( 1, false ) ); + } +}