9

I know how to retrieve the rss2 feed from a category in wp, namely adding /feed behind the category name. However when I do this, not all entries from that feed are included in the rss, this is because I set the feed restriction to be 50 at the admin backend.

I would like to know if a feed length could be set indiviually for each category/feed programatically.

I could not find any api settings for the size of the feeds ... any help is welcome here.

Thanks for your help.

1
  • Stephen Harris and CodeMonkey the answers both do not work for one particular category? Otherwise both answers work do work for me! Why could it be that it only does not work for one category? Nov 15, 2012 at 15:17

2 Answers 2

8
+50

You should be able to use pre_get_posts and set the posts_per_page to 50 conditionally (on being feed for categories). Unfortunately there is this unresolved trac ticket.

The only work-around is to do hook into post_limits and replace the LIMIT part of the SQL query directly.

add_action('post_limits','wpse71759_category_rss_limit',10,2);
function wpse71759_category_rss_limit($limit, $query){

     if( $query->is_feed() && $query->is_category() ){
          $paged =  $query->get('paged') ? (int) $query->get('paged') : 1;
          $per_page = 50;
          $page_start = ($paged-1)*$per_page;

          return "LIMIT $page_start, $per_page";
     }
     return $limit;
}
8
  • Hey Stephen I will look into that, uhm actually I always want all entries from a category in a feed, and only on my blog page I want a restriction of the limit that I set in the backend, how would I do that? Nov 13, 2012 at 1:12
  • In that case, return ""; rather than "LIMIT $page_start, $per_page" should do it. Nov 13, 2012 at 10:41
  • Hey can you explain your code a bit? I am not quite sure if that is what I want what that you are offering here. Nov 15, 2012 at 14:42
  • So for category feeds you want to return all posts - so you don't want a LIMIT part to your query. The above filter, allows you to replace the LIMIT part with a blank string, so that the resulting sql will fetch all posts. Nov 15, 2012 at 14:45
  • 1
    Yes, but I'd recommend making a plug-in out of it (wpcandy.com/teaches/how-to-create-a-functionality-plugin) Nov 15, 2012 at 14:54
2
add_filter('query', 'sql_query_feed_filter');
function sql_query_feed_filter($sql){
    global $wp_query;

    if ($wp_query->is_feed == true &&  isset($wp_query->query_vars['custom_taxonomy']) &&  in_array($wp_query->query_vars['custom_taxonomy'], array('slug1','slug2')) ){
        if (strpos($sql, 'SELECT SQL_CALC_FOUND_ROWS') !== false){
            list($sql,) = explode('LIMIT', $sql);
        }
    }
    return $sql;
}

all you need to do is just edit this function and repalce taxonomy by your category taxonomy and edit slug to much your category...

7
  • Would you care to explain your code? Nov 14, 2012 at 23:12
  • Simple SQL Query filter that trigger on feed ($wp_query->is_feed == true) if specific taxonomy found ($wp_query->query_vars['custom_taxonomy'] custom_taxonomy in example) and if it quivaluent to some slug ( $wp_query->query_vars['custom_taxonomy'] == 'slug' ). if everything of this happens and true (on a site.com/your-custom-feed-for-category.xml ) you will get no limit results in your feed ($SQL statment divided by LIMIT value which mean it returned without last LIMIT operator... no limit. all entries per rss.) Nov 15, 2012 at 9:16
  • Where would I drop that code? functions.php of my theme? Nov 15, 2012 at 9:34
  • functions.php perfactly fits. but change slug and name of custom taxonomy before. and clear browser cache before testing (RSS Feeds a cached by browser). Nov 15, 2012 at 9:35
  • Hey thanks for the heads up with the cache! It kind of works, but still on at least one category I will not get all posts in the feed, on others it works ... Nov 15, 2012 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.