WordPress.org

Ready to get started?Download WordPress

Forums

set limit of post in rss feed (14 posts)

  1. xorax
    Member
    Posted 7 years ago #

    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!!

  2. Glenn Ansley
    Member
    Posted 7 years ago #

    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.

  3. xorax
    Member
    Posted 7 years ago #

    yes but I want to set it temporary in a plugin

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  5. xorax
    Member
    Posted 7 years ago #

    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 ???

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  7. xorax
    Member
    Posted 7 years ago #

    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...

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  9. xorax
    Member
    Posted 7 years ago #

    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!

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  11. xorax
    Member
    Posted 7 years ago #

    humm I don't know why but your solution doesn't work...

  12. xorax
    Member
    Posted 7 years ago #

    no soory it work :)

  13. xorax
    Member
    Posted 7 years ago #

    but not :

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

    is_feed() == false :(

  14. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic