From 65932f71fdb77441d8cbdbb34bedf96cd83c92a1 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 7 Mar 2014 18:32:40 +0000 Subject: [PATCH] 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 --- src/wp-includes/query.php | 10 +++++++--- tests/phpunit/tests/query.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index d251ef1763..e8c95caf68 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -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']; diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 18f23d83a6..98a66dbb3a 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -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 ); + } } \ No newline at end of file