From b2cbe4b2f56306ca19c1196ce648a148094259d0 Mon Sep 17 00:00:00 2001 From: yashjawale Date: Wed, 4 Nov 2026 13:38:58 +0530 Subject: [PATCH] feat: add pre_get_lastpostdate filter to short-circuit get_lastpostdate() query --- src/wp-includes/post.php | 18 ++++++++ tests/phpunit/tests/post/getLastPostDate.php | 47 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 5806d879f1c5e..069c05c1459fb 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -7998,6 +7998,24 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, * @return string The date of the last post, or false on failure. */ function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { + /** + * Pre-filter the return value of get_lastpostdate() before the query is run. + * + * @since 7.3.0 + * + * @param string|false $lastpostdate The most recent time that a post was published, + * in 'Y-m-d H:i:s' format, or false. Returning anything + * other than false will short-circuit the function. + * @param string $timezone Location to use for getting the post published date. + * See get_lastpostdate() for accepted `$timezone` values. + * @param string $post_type The post type to check. + */ + $lastpostdate = apply_filters( 'pre_get_lastpostdate', false, $timezone, $post_type ); + + if ( false !== $lastpostdate ) { + return $lastpostdate; + } + $lastpostdate = _get_last_post_time( $timezone, 'date', $post_type ); /** diff --git a/tests/phpunit/tests/post/getLastPostDate.php b/tests/phpunit/tests/post/getLastPostDate.php index de2239cdbbaa8..c3ad9612168e6 100644 --- a/tests/phpunit/tests/post/getLastPostDate.php +++ b/tests/phpunit/tests/post/getLastPostDate.php @@ -61,4 +61,51 @@ public function test_get_lastpostdate() { $this->assertSame( $post_post_date_last, get_lastpostdate( 'blog', 'post' ) ); $this->assertSame( $book_post_date_last, get_lastpostdate( 'blog', 'book' ) ); } + + /** + * @ticket 63614 + */ + public function test_pre_get_lastpostdate_filter_short_circuits_query() { + global $wpdb; + + self::factory()->post->create( + array( + 'post_date' => '2020-02-28 16:09:28', + ) + ); + + $filter = new MockAction(); + add_filter( 'pre_get_lastpostdate', array( $filter, 'filter' ), 10, 3 ); + add_filter( + 'pre_get_lastpostdate', + static function () { + return '2001-01-01 00:00:00'; + } + ); + + $num_queries = $wpdb->num_queries; + $result = get_lastpostdate( 'blog', 'post' ); + + $this->assertSame( '2001-01-01 00:00:00', $result, 'The filtered value should be returned.' ); + $this->assertSame( $num_queries, $wpdb->num_queries, 'No database queries should be run.' ); + $this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' ); + $this->assertSame( array( false, 'blog', 'post' ), $filter->get_args()[0], 'The filter should receive the default value, timezone, and post type.' ); + } + + /** + * @ticket 63614 + */ + public function test_pre_get_lastpostdate_filter_returning_false_does_not_short_circuit() { + $post_date = '2020-02-28 16:09:28'; + + self::factory()->post->create( + array( + 'post_date' => $post_date, + ) + ); + + add_filter( 'pre_get_lastpostdate', '__return_false' ); + + $this->assertSame( $post_date, get_lastpostdate( 'blog', 'post' ) ); + } }