diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index cf07b07d977c3..325349f06f03b 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -2000,18 +2000,20 @@ public function get_posts() { } } $post_type = $query_vars['post_type']; - if ( empty( $query_vars['posts_per_page'] ) ) { - $query_vars['posts_per_page'] = get_option( 'posts_per_page' ); + if ( isset( $query_vars['posts_per_page'] ) && ( is_numeric( $query_vars['posts_per_page'] ) || is_bool( $query_vars['posts_per_page'] ) ) ) { + $query_vars['posts_per_page'] = $query_vars['posts_per_page']; + } else { + $query_vars['posts_per_page'] = get_option( 'posts_per_page', 10 ); } if ( isset( $query_vars['showposts'] ) && $query_vars['showposts'] ) { $query_vars['showposts'] = (int) $query_vars['showposts']; $query_vars['posts_per_page'] = $query_vars['showposts']; } - if ( ( isset( $query_vars['posts_per_archive_page'] ) && 0 != $query_vars['posts_per_archive_page'] ) && ( $this->is_archive || $this->is_search ) ) { + if ( isset( $query_vars['posts_per_archive_page'] ) && ( is_numeric( $query_vars['posts_per_archive_page'] ) || is_bool( $query_vars['posts_per_archive_page'] ) ) && ( $this->is_archive || $this->is_search ) ) { $query_vars['posts_per_page'] = $query_vars['posts_per_archive_page']; } if ( ! isset( $query_vars['nopaging'] ) ) { - if ( -1 == $query_vars['posts_per_page'] ) { + if ( -1 == (int) $query_vars['posts_per_page'] ) { $query_vars['nopaging'] = true; } else { $query_vars['nopaging'] = false; @@ -2031,8 +2033,6 @@ public function get_posts() { $query_vars['posts_per_page'] = (int) $query_vars['posts_per_page']; if ( $query_vars['posts_per_page'] < -1 ) { $query_vars['posts_per_page'] = abs( $query_vars['posts_per_page'] ); - } elseif ( 0 === $query_vars['posts_per_page'] ) { - $query_vars['posts_per_page'] = 1; } if ( ! isset( $query_vars['comments_per_page'] ) || 0 == $query_vars['comments_per_page'] ) { @@ -3718,7 +3718,7 @@ private function set_found_posts( $query_vars, $limits ) { */ $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); - if ( ! empty( $limits ) ) { + if ( ! empty( $limits ) && 0 != $query_vars['posts_per_page'] ) { $this->max_num_pages = (int) ceil( $this->found_posts / $query_vars['posts_per_page'] ); } } diff --git a/tests/phpunit/tests/query/postsPerPage.php b/tests/phpunit/tests/query/postsPerPage.php new file mode 100644 index 0000000000000..a698c4100bca4 --- /dev/null +++ b/tests/phpunit/tests/query/postsPerPage.php @@ -0,0 +1,147 @@ +post->create_many( 11 ); + } + + public function set_up() { + parent::set_up(); + unset( $this->q ); + $this->q = new WP_Query(); + } + + public function _get_post_count( $args = array() ) { + $args = wp_parse_args( + $args, + array( + 'fields' => 'ids', + ) + ); + + return count( $this->q->query( $args ) ); + } + + public function test_posts_per_page_integer_positive() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => 2, + ) + ); + + $this->assertSame( 2, $count ); + } + + /** + * @ticket 24142 + */ + public function test_posts_per_page_integer_zero() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => 0, + ) + ); + + $this->assertSame( 0, $count ); + } + + public function test_posts_per_page_string_numeric() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => '2', + ) + ); + + $this->assertSame( 2, $count ); + } + + public function test_posts_per_page_string_non_numeric() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => 'foo', + ) + ); + + $this->assertSame( 10, $count ); + } + + public function test_posts_per_page_boolean_true() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => true, + ) + ); + + $this->assertSame( 1, $count ); + } + + /** + * @ticket 24142 + */ + public function test_posts_per_page_boolean_false() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => false, + ) + ); + + $this->assertSame( 0, $count ); + } + + public function test_posts_per_page_null() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => null, + ) + ); + + $this->assertSame( 10, $count ); + } + + public function test_posts_per_page_empty_string() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => '', + ) + ); + + $this->assertSame( 10, $count ); + } + + public function test_posts_per_page_array() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => array(), + ) + ); + + $this->assertSame( 10, $count ); + } + + public function test_posts_per_page_negative() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => -2, + ) + ); + + $this->assertSame( 2, $count ); + } + + public function test_posts_per_page_all() { + $count = $this->_get_post_count( + array( + 'posts_per_page' => -1, + ) + ); + + $this->assertSame( 11, $count ); + } +}