How to change the quantity of feeds in custom post type?

I created a custom post type “podcast” and all podcasts are inside it. when I visit http://example.com/?feed=podcast, it only shows latest 10 podcasts. How do I change it to 99?

I googled and found the code below:

function feedFilter($query) {
if (is_feed()) {
    $query->set('posts_per_page','30');
}
return $query;}
add_filter('pre_get_posts', 'feedFilter');

It doesn’t work.

How to customize the feed items quantity in a custom post type?

3 Answers
3

The file where the pre_get_posts action runs is wp-includes/query.php, If we look in that file, we’ll see that after the action is executed, the posts_per_page setting is overridden for feeds with this bit of code:

if ( $this->is_feed ) {
    $q['posts_per_page'] = get_option('posts_per_rss');
    $q['nopaging'] = false;
}

In this case, we can add a filter to the value of posts_per_rss to customize the feed quantity that way.

function feed_action( $query ){
    if( $query->is_feed( 'podcast' ) ) {
        add_filter( 'option_posts_per_rss', 'feed_posts' );
    }
}
add_action( 'pre_get_posts', 'feed_action' );

function feed_posts(){
    return 99;
}

Leave a Comment