WordPress.org

Make WordPress Core

Changeset 27455


Ignore:
Timestamp:
03/07/2014 06:32:40 PM (6 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r27395 r27455  
    22492249        }
    22502250        $post_type = $q['post_type'];
    2251         if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
    2252             $q['posts_per_page'] = get_option('posts_per_page');
     2251        if ( ! isset( $q['posts_per_page'] ) || $q['posts_per_page'] == 0 ) {
     2252            if ( $this->is_feed ) {
     2253                $q['posts_per_page'] = get_option( 'posts_per_rss' );
     2254            } else {
     2255                $q['posts_per_page'] = get_option( 'posts_per_page' );
     2256            }
     2257        }
    22532258        if ( isset($q['showposts']) && $q['showposts'] ) {
    22542259            $q['showposts'] = (int) $q['showposts'];
     
    22652270        }
    22662271        if ( $this->is_feed ) {
    2267             $q['posts_per_page'] = get_option('posts_per_rss');
    22682272            $q['nopaging'] = false;
    22692273        }
  • trunk/tests/phpunit/tests/query.php

    r27304 r27455  
    8888        $this->assertTrue( $query->get( 'nonexistent', true ) );
    8989    }
     90
     91    /**
     92     * @ticket 25380
     93     */
     94    function test_pre_posts_per_page() {
     95        $this->factory->post->create_many( 10 );
     96
     97        add_action( 'pre_get_posts', array( $this, 'filter_posts_per_page' ) );
     98
     99        $this->go_to( get_feed_link() );
     100
     101        $this->assertEquals( 30, get_query_var( 'posts_per_page' ) );
     102    }
     103
     104    function filter_posts_per_page( &$query ) {
     105        $query->set( 'posts_per_page', 30 );
     106    }
    90107}
Note: See TracChangeset for help on using the changeset viewer.