Skip to content

Commit 818a0f1

Browse files
committed
Query: Fix warning on counting non countable
Adds tests to continue the behavior for both null and strings. See https://wiki.php.net/rfc/counting_non_countables for information on the PHP change. Fixes #42860. Props janak007 and ayeshrajans for initial patches. git-svn-id: https://develop.svn.wordpress.org/trunk@42581 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f324349 commit 818a0f1

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3158,7 +3158,15 @@ private function set_found_posts( $q, $limits ) {
31583158
*/
31593159
$this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
31603160
} else {
3161-
$this->found_posts = count( $this->posts );
3161+
if ( is_array( $this->posts ) ) {
3162+
$this->found_posts = count( $this->posts );
3163+
} else {
3164+
if ( null === $this->posts ) {
3165+
$this->found_posts = 0;
3166+
} else {
3167+
$this->found_posts = 1;
3168+
}
3169+
}
31623170
}
31633171

31643172
/**

tests/phpunit/tests/post/query.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,4 +642,45 @@ public function test_set_found_posts_fields_not_split_the_query() {
642642
$this->assertEquals( 2, $q->found_posts );
643643
$this->assertEquals( 2, $q->max_num_pages );
644644
}
645+
646+
/**
647+
* @ticket 42860
648+
*/
649+
public function test_set_found_posts_fields_posts_is_string() {
650+
$q = new WP_Query(
651+
array(
652+
'post_type' => 'wptests_pt',
653+
'posts_per_page' => 1,
654+
)
655+
);
656+
657+
$q->posts = "To life, to life, l'chaim";
658+
659+
$methd = new \ReflectionMethod( 'WP_Query', 'set_found_posts' );
660+
$methd->setAccessible( true );
661+
$methd->invoke( $q, array( 'no_found_rows' => false ), array() );
662+
663+
$this->assertEquals( 1, $q->found_posts );
664+
}
665+
666+
/**
667+
* @ticket 42860
668+
*/
669+
public function test_set_found_posts_fields_posts_is_null() {
670+
$q = new WP_Query(
671+
array(
672+
'post_type' => 'wptests_pt',
673+
'posts_per_page' => 1,
674+
)
675+
);
676+
677+
$q->posts = null;
678+
679+
$methd = new \ReflectionMethod( 'WP_Query', 'set_found_posts' );
680+
$methd->setAccessible( true );
681+
$methd->invoke( $q, array( 'no_found_rows' => false ), array() );
682+
683+
$this->assertEquals( 0, $q->found_posts );
684+
}
685+
645686
}

0 commit comments

Comments
 (0)