Skip to content

Commit 450524a

Browse files
Posts, Post Types: Avoid an extra database query in WP_Post for a negative post ID.
Follow-up to [21559]. Props bor0, akeda, SergeyBiryukov. Fixes #63850. git-svn-id: https://develop.svn.wordpress.org/trunk@60654 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ab52706 commit 450524a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/wp-includes/class-wp-post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static function get_instance( $post_id ) {
233233
global $wpdb;
234234

235235
$post_id = (int) $post_id;
236-
if ( ! $post_id ) {
236+
if ( $post_id <= 0 ) {
237237
return false;
238238
}
239239

tests/phpunit/tests/post/wpPost.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public function test_get_instance_should_fail_for_negative_number() {
4141
$this->assertFalse( $found );
4242
}
4343

44+
/**
45+
* @ticket 63850
46+
*/
47+
public function test_get_instance_should_not_perform_database_query_for_negative_number() {
48+
$num_queries = get_num_queries();
49+
$found = WP_Post::get_instance( -self::$post_id );
50+
51+
$this->assertSame( $num_queries, get_num_queries() );
52+
}
53+
4454
/**
4555
* @ticket 37738
4656
*/

0 commit comments

Comments
 (0)