Support » Plugins » set limit of post in rss feed

  • I’m french so sorry for my bad english 🙂

    How can I set the limit of post display in rss2 ?
    I try :

    if(!function_exists('ongetfeed')) {
      function ongetfeed (){
        if(!empty($_GET['nolimit']) ){
          add_filter('post_limits',"setpost_limits");
        }
      }
    }
    
    if(!function_exists('setpost_limits')) {
      function setpost_limits ($s){
        return '100000';
      }
    }
    
    add_action('do_feed_rss2','ongetfeed',1);
    add_action('rss2_head','ongetfeed',1);
    add_action('rss2_ns','ongetfeed',1);

    I worked many time on this with many others solutions… and I have no result at the time…

    HELP ME!!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Are you trying to do something different than what you could do in options –> reading. You can set the # of post for the RSS to display there.

    yes but I want to set it temporary in a plugin

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You want no limit at all? Change this:

    function setpost_limits ($s){
        return '100000';
      }

    To this:

    function setpost_limits ($s){
        return '';
      }

    If you really want a 100000 limit, then you need to do:
    return 'LIMIT 100000';
    instead.

    no change…
    the filter ‘post_limits’ does not modify the request.

    If I watch all globals var (print_r($GLOBALS)) in hook function, I can see :

    [last_query] => SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN(156,155,154,149,148,146,145,144,143,142) ORDER BY post_id, meta_key

    so reset limit is apparently not the solution… but what is it ???

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You’re looking at the wrong query. That one is getting the postmeta information for updating, not the posts themselves. What’s the query actually getting the posts from wp_posts?

    The post_limits filter *does* work.

    it’s right, some error 🙂

    I have define(‘SAVEQUERIES’, true); and I can view all query.

    SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND (post_type = 'post' AND (post_status = 'publish' OR post_status = 'private')) ORDER BY post_date DESC LIMIT 0, 10

    but post_limits does not work again…

    When print_r($GLOBALS) is run, (in function ongetfeed()) this query is already run and appear in
    [wpdb][queries][7][0]

    so, when the first hook action is running (I think ‘do_feed_rss2’), the request has been already run ! and so my filter has no way…

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Oh. Here’s your problem:

    add_action('do_feed_rss2','ongetfeed',1);
    add_action('rss2_head','ongetfeed',1);
    add_action('rss2_ns','ongetfeed',1);

    That won’t work in a plugin. By the time those actions happen, it’s too late. Your post query has executed way before it ever gets to the rss2 actions.

    Try this instead of all of the code you gave in the first post:

    if (is_feed() && !empty($_GET['nolimit']) ){
    add_filter('post_limits',"setpost_limits");
    }
    if(!function_exists('setpost_limits')) {
      function setpost_limits ($s){
        return '';
      }
    }

    That will add the action when the plugin is loaded, which is before the query executes.

    ok, I have find the action who call the hook function just before the query, it’s ‘pre_get_posts’ :

    if(!function_exists('ongetfeed')) {
      function ongetfeed (){
        if(is_feed() && !empty($_GET['nolimit']) ){
          add_filter('post_limits',"setpost_limits");
        }
      }
    }
    
    if(!function_exists('setpost_limits')) {
      function setpost_limits ($s){
        return '';
      }
    }
    add_action('pre_get_posts','ongetfeed',1);

    and it’s all right!

    really thanks Otto42

    this is my first plugin experience in wordpress, and I notice that the facility of installation for the users is reflected on the difficulty of coding… And the online documentation is very bad… think more/some debugs functions should be implement.

    for informations my plugin will download the associed sounds of ReadSpeaker (feed talker) and implement them in the posts.

    thanks again!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    That’s a really weird way of doing things. You’re hooking into an action just to hook into a filter. Why go to all that trouble? Just hook the filter in. Like this:

    function setpost_limits ($s) {
      if(is_feed() && !empty($_GET['nolimit']) ) return '';
      else return $s;
    }
    add_filter('post_limits','setpost_limits');

    Simpler.

    humm I don’t know why but your solution doesn’t work…

    no soory it work 🙂

    but not :

    if (is_feed() && !empty($_GET['nolimit']) ){
    add_filter(....

    is_feed() == false 🙁

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    is_feed() == false 🙁

    is_feed() always returns true when you’re actually accessing a feed (any feed). It won’t work on a normal blog page.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘set limit of post in rss feed’ is closed to new replies.