InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress RSS feed tutorials

  • create rss feed for wordpress on Google feedburner
  • use your own domain name for feedburner feed
  • redirect wordpress feed to feedburner feed
  • setup Google feedburner email subscription newsletter
  • Google feedburner email subscription vs Mailchimp
  • setup MailChimp Rss email campaign
  • ping feedburner to force update rss cache
  • customize wordpress rss feed
  • Fetch wordpress rss feed as FeedBurner user agent
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress RSS feed

How to customize wordpress rss feed

By admin on Nov 19, 2015

By defaul WordPress will include all posts sorted by creation time for rss feed. If you want to customize wordpress feed, you can write custom code to include wordpress pages, skip specific entries, etc. Here are steps to customize wordpress feed for few cases:

Include pages in wordpress feed

To incluse wordpress pages to feed, we can use pre_get_posts hook as shown below:

function pre_get_posts_rss ($query) {
  if (is_feed() ) {
    $query->set('post_type', array('post', 'page'));
  }
}
add_action('pre_get_posts', 'pre_get_posts_rss');

Change posts per rss wordpress feed

To change posts per rss, we can add filter to pre_option_posts_per_rss and set the desired value as shown below:

add_filter('pre_option_posts_per_rss', function() { return 30;});

Skip certain posts based on meta key

In case you want to skip some post based on meta key or other custom logic, you can attach hook to posts_results. In the following code we’ll skip post which have meta key skip_rss. We’ll also skip non-leaf pages.

function posts_results_rss( $posts ) {
  if (!is_feed() ) {
    return $posts;
  }
  $filtered_posts = array();
  foreach ( $posts as $post ) {
    $skip_rss = get_post_meta($post->ID, 'skip_rss', true);
    $is_leaf = _is_leaf_page($post->ID);
    if (!$skip_rss && $is_leaf) {
      $filtered_posts[] = $post;
    }
    if (count($filtered_posts) >= 10) {
      break;
    }
  }
  return $filtered_posts ;
}
add_filter( 'posts_results', 'posts_results_rss' );

Note that for this logic to work properly, you need to increase posts per rss so that we get enough entries in function posts_results_rss. Implementation for function _is_leaf_page can see seen in article Find if a wordpress is leaf.

Suggested posts:

  1. find if a wordpress page is leaf page
  2. WordPress – customize facebook plugin opengraph meta tags
  3. How to customize wordpress image alt tag
  4. Fetch wordpress rss feed as FeedBurner user agent on command line
  5. How to hide a post from home and RSS feed in wordpress
  6. How to redirect wordpress feed to feedburner feed url
  7. How to remove xmlrpc from wordpress headers
  8. How to create rss feed for wordpress blog on Google feedburner
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Tutorials, Ubuntu Linux, Web Development, Wordpress, Wordpress RSS feed

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2021 InfoHeap.

Powered by WordPress