When setting posts_per_page in WP_Query::get_posts(), check for is_feed() in the same place where posts_per_page is set when it is not already set. Previously, when is_feed() was true, posts_per_page would be arbitrarily overwritten. This fix allows posts_per_page to be set during pre_get_posts when is_feed() is true and not be overwritten.

Adds unit test.

Props wokamoto.
Fixes #25380.



git-svn-id: https://develop.svn.wordpress.org/trunk@27455 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-03-07 18:32:40 +00:00
parent a8bc73c543
commit 65932f71fd
2 changed files with 24 additions and 3 deletions

View File

@ -2248,8 +2248,13 @@ class WP_Query {
$q['post_type'] = '';
}
$post_type = $q['post_type'];
if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
$q['posts_per_page'] = get_option('posts_per_page');
if ( ! isset( $q['posts_per_page'] ) || $q['posts_per_page'] == 0 ) {
if ( $this->is_feed ) {
$q['posts_per_page'] = get_option( 'posts_per_rss' );
} else {
$q['posts_per_page'] = get_option( 'posts_per_page' );
}
}
if ( isset($q['showposts']) && $q['showposts'] ) {
$q['showposts'] = (int) $q['showposts'];
$q['posts_per_page'] = $q['showposts'];
@ -2264,7 +2269,6 @@ class WP_Query {
}
}
if ( $this->is_feed ) {
$q['posts_per_page'] = get_option('posts_per_rss');
$q['nopaging'] = false;
}
$q['posts_per_page'] = (int) $q['posts_per_page'];

View File

@ -87,4 +87,21 @@ class Tests_Query extends WP_UnitTestCase {
$this->assertFalse( $query->get( 'nonexistent', false ) );
$this->assertTrue( $query->get( 'nonexistent', true ) );
}
/**
* @ticket 25380
*/
function test_pre_posts_per_page() {
$this->factory->post->create_many( 10 );
add_action( 'pre_get_posts', array( $this, 'filter_posts_per_page' ) );
$this->go_to( get_feed_link() );
$this->assertEquals( 30, get_query_var( 'posts_per_page' ) );
}
function filter_posts_per_page( &$query ) {
$query->set( 'posts_per_page', 30 );
}
}