Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#25380 closed defect (bug) (fixed)

Allow posts_per_page option for pre_get_posts action hook on feed

Reported by: wokamoto's profile wokamoto Owned by: wonderboymusic's profile wonderboymusic
Milestone: 3.9 Priority: normal
Severity: normal Version: 3.6.1
Component: Query Keywords: has-patch needs-unit-tests
Focuses: Cc:

Description

This is a patch to fix an issue where posts_per_page option for pre_get_posts action hook does not work while is_feed() is true.

For example, this code can't change the number of posts per page in a feed.

<?php
function my_pre_get_posts_for_feed( $query ) {
  if ( is_admin() || ! $query->is_main_query() )
    return;

  if ( is_feed() ) {
    // Display 50 posts for the feed
    $query->set( 'posts_per_page', 50 );
  }
}
add_action( 'pre_get_posts', 'my_pre_get_posts_for_feed', 1 );

Attachments (3)

query.diff (965 bytes) - added by wokamoto 11 years ago.
25380.diff (592 bytes) - added by nacin 11 years ago.
25380.2.diff (1.0 KB) - added by nacin 11 years ago.

Download all attachments as: .zip

Change History (9)

@wokamoto
11 years ago

#1 @wokamoto
11 years ago

  • Keywords has-patch added

#2 @nacin
11 years ago

  • Component changed from General to Query

Thanks for this, wokamoto. This sounds familiar — there may be a similar bug report I couldn't find.

#3 @wonderboymusic
11 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 3.9

I am going to write a few tests for this.

#4 @wonderboymusic
11 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 27455:

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.

@nacin
11 years ago

This ticket was mentioned in IRC in #wordpress-dev by nacin. View the logs.


11 years ago

@nacin
11 years ago

#6 @wonderboymusic
11 years ago

In 27456:

On second thought, allow posts_per_rss to be set in pre_get_posts to override the posts_per_rss option. There are backwards compatibility concerns otherwise.

Props nacin.
Partially reverts [27455]. Updates the unit test. See #25380.

Note: See TracTickets for help on using tickets.