WordPress.org

Make WordPress Core

Opened 7 months ago

Closed 6 weeks ago

Last modified 6 weeks ago

#25380 closed defect (bug) (fixed)

Allow posts_per_page option for pre_get_posts action hook on feed

Reported by: wokamoto Owned by: 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 7 months ago.
25380.diff (592 bytes) - added by nacin 6 weeks ago.
25380.2.diff (1.0 KB) - added by nacin 6 weeks ago.

Download all attachments as: .zip

Change History (9)

wokamoto7 months ago

comment:1 wokamoto7 months ago

  • Keywords has-patch added

comment:2 nacin3 months 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.

comment:3 wonderboymusic2 months ago

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

I am going to write a few tests for this.

comment:4 wonderboymusic6 weeks 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.

nacin6 weeks ago

comment:5 ircbot6 weeks ago

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

nacin6 weeks ago

comment:6 wonderboymusic6 weeks 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.