WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 4 years 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 5 years ago.
25380.diff (592 bytes) - added by nacin 4 years ago.
25380.2.diff (1.0 KB) - added by nacin 4 years ago.

Download all attachments as: .zip

Change History (9)

@wokamoto
5 years ago

#1 @wokamoto
5 years ago

  • Keywords has-patch added

#2 @nacin
4 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
4 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
4 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
4 years ago

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


4 years ago

@nacin
4 years ago

#6 @wonderboymusic
4 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.