Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'] ) {
Expand Down Expand Up @@ -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'] );
}
}
Expand Down
147 changes: 147 additions & 0 deletions tests/phpunit/tests/query/postsPerPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/**
* @group query
*/
class Tests_Query_PostsPerPage extends WP_UnitTestCase {
public $q;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// More than posts_per_page default of 10
// This number is verified in the 'test_posts_per_page_all' method.
self::factory()->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 );
}
}
Loading