3

I need to override the setting in the WP Admin for the number of recent posts in the Syndication settings under General -> Admin.

I'm using the following code, which gets all posts, but I don't need that many. Can someone give me an example of how to retrieve 50 posts? This should only affect this one feed and not all the others.

function no_limits_for_feed( $limits ) {
    return '';
}

add_filter( 'post_limits', 'no_limits_for_feed' );

UPDATE: Here's an example of the code I am using, which is from the Feed JSON WordPress plug-in:

class feed_json {
    function feed_json() {
        global $wp_rewrite;

        add_action('init', array(&$this, 'add_feed_json'));
        add_action('do_feed_json', array(&$this, 'do_feed_json'), 10, 1);
        add_filter('template_include', array(&$this, 'template_json'));
        add_filter('query_vars', array(&$this, 'add_query_vars'));

        $plugin_basename = plugin_basename(__FILE__);
        add_action('activate_' . $plugin_basename, array(&$this, 'add_feed_json_once'));
        add_action('deactivate_' . $plugin_basename, array(&$this, 'remove_feed_json'));
    }

    function add_feed_json_once() {
        global $wp_rewrite;
        $this->add_feed_json();
        $wp_rewrite->flush_rules();
    }

    function remove_feed_json() {
        global $wp_rewrite;
        $feeds = array();
        foreach ( $wp_rewrite->feeds as $feed ) {
            if ( $feed !== 'json' ) {
                $feeds[] = $feed;
            }
        }
        $wp_rewrite->feeds = $feeds;
        $wp_rewrite->flush_rules();
    }

    function add_query_vars($qvars) {
      $qvars[] = 'callback';
      $qvars[] = 'limit';
      return $qvars;
    }

    function add_feed_json() {
        add_feed('json', array(&$this, 'do_feed_json'));
    }

    function do_feed_json() {
        load_template($this->template_json(dirname(__FILE__) . '/feed-json-template.php'));
    }

    function template_json( $template ) {
        $template_file = false;
        if (get_query_var('feed') === 'json') {
            $template_file = '/feed-json.php';
            if (function_exists('get_stylesheet_directory') && file_exists(get_stylesheet_directory() . $template_file)) {
                $template_file = get_stylesheet_directory() . $template_file;
            } elseif (function_exists('get_template_directory') && file_exists(get_template_directory() . $template_file)) {
                $template_file = get_template_directory() . $template_file;
            } elseif (file_exists(dirname(__FILE__) . '/feed-json-template.php')) {
                $template_file = dirname(__FILE__) . '/feed-json-template.php';
            } else {
                $template_file = false;
            }
        }

        return (
            $template_file !== false
            ? $template_file
            : $template
            );
    }
}
new feed_json();
Zoolander
  • 2,293
  • 5
  • 27
  • 37

1 Answers1

2

I thought you could hook into pre_get_posts, but if it is a feed the posts_per_page value gets overwritten later. So just hook into post_limits, check for your feed, and return a different LIMIT part if needed.

add_filter( 'post_limits', 'so6230475_post_limits', 10, 2 );
function so6230475_post_limits( $limits, &$wp_query )
{
    if ( $wp_query->is_feed( 'json' ) ) {
        $limits = 'LIMIT 50';
    }
    return $limits;
}
Jan Fabry
  • 7,221
  • 2
  • 36
  • 41
  • Can you tell me how to set a flag for "/* check for your special feed */"? – Zoolander Jun 08 '11 at 21:28
  • @Zoolander: This depends on how you created the feed. Can you update your question with information how you did that, and also the code? – Jan Fabry Jun 09 '11 at 05:51
  • @Jan Fabry: I've updated my original post with the code and also included a link to the plug-in I'm using. THANKS! – Zoolander Jun 09 '11 at 14:37
  • @Zoolander: If you're adding an extra feed type, you can just write `if ( $wp_query->is_feed( 'json' ) )`, which will only be true if it is a feed and if it's from the type `json`. – Jan Fabry Jun 09 '11 at 16:31
  • @Jan Fabry: I added that to the IF statement, however it's still only returning 5 results for the feed. I have "Syndication feeds show the most recent" set to 5 in my WP Admin and posts_per_page in functions.php is set to 100. Any ideas? – Zoolander Jun 09 '11 at 16:37
  • @Jan Fabry: After looking at the [WordPress Docs](http://codex.wordpress.org/Function_Reference/query_posts) (look under Syndication Feeds) on `posts_per_page`, it looks as though the database setting will override `posts_per_page`. They say to use `post_limits` / , `no_limits_for_feed` but that would return 300 items if there are 300 posts in the database when I only want 50. :( Any ideas? – Zoolander Jun 09 '11 at 16:48
  • @Zoolander: You're right, it gets overwritten. I rewrote my answer, it now modifies `post_limits` directly. – Jan Fabry Jun 09 '11 at 18:48
  • Wow Jan Fabry -- you RoCk!!!! I was beginning to think this wasn't going to be possible. Your solution works great! THANK YOU!!!! – Zoolander Jun 09 '11 at 19:03