I use wp as a cms. I needed a custom-made template for my rss feeds, so that I could display custom fields of my posts in the feeds. I took the basic custom rss template suggested by Yoast and made some modification as I needed. It is working fine except that I am unable to retrieve the featured-post-thumbnail and other custom-field-values.

Here is what my custom rss feed url looks like now - http://website.com/custom-feed/

Here is the complete code :

Template Name: Custom Feed 

$numposts = 5;
function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);

$posts = query_posts('showposts='.$numposts);
$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?><rss version="2.0">
<title>website content feeds</title>
<description>The latest content from website.</description>
<pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
<lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
<managingEditor>[email protected]</managingEditor>
<?php foreach ($posts as $post) { ?>

<title><?php echo get_the_title($post->ID); ?></title>
<link><?php echo get_permalink($post->ID); ?></link>

<?php echo add_my_custom_field_node();?>

<description><?php $object = new WPSEO_Frontend(); echo $object->metadesc( false ); ?> </description>
<pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
<guid><?php echo get_permalink($post->ID); ?></guid>

<?php } ?>

The code that I am trying to pull the thumbnail is suggested in a solution by @bainternet to a similar question and I have it between the <link> and <description> tags. But it is not doing anything.

I tried using <?php echo get_post_meta($post->ID, 'location', true);?> for custom-feld-values to appear but it does not work either. Is there specific tags I should be using for the featured-image or custom-fields to appear, if so what tags and how do I use them? I also tried solution suggested here. What am I missing. Please guide, thanks.

FYI - My post descriptions are auto generated by the yoast-wordpress-seo plugin so for descriptions I am using a custom code, which is working fine.

UPDATE : Reading the solution by @BenMiller to this question I understood that I have to use rss2_item action hook in order to add tags to my custom-rss-feed. Here is a the code in my functions.php now :

 add_action('rss2_item', 'add_my_custom_field_node');

function add_my_custom_field_node() {
    global $post;
    $metaValue = get_post_meta($post->ID, 'location', true);

SECOND UPDATE : The other strange thing that I noticed is that when I checked my custom-rss-feed-url at validator.org. It shows my custom-field-values and the featured-image url with a .jpg extension but for some reason throws an error. Undefined item element: my-custom-field. I use livebookmarks to check feed-url and using firefox browser. I am totally confused.

  • from your file, remove this line <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); }?> and add this one - if( has_post_thumbnail($post->ID) ){ $thumbnail = get_attachment_link(get_post_thumbnail_id($post->ID)); echo("<image>{$thumbnail}</image>"); }
    – Shazzad
    Commented Jan 20, 2014 at 9:30
  • @Shazzad I did the suggested but still cant see any image in the feeds. FYI - I have not added anything to my funtions.php yet.
    – gurung
    Commented Jan 20, 2014 at 9:36
  • Can you give me a link to you feed page ?
    – Shazzad
    Commented Jan 20, 2014 at 9:53
  • Please add add_my_rss_node() action in functions.php
    – shahpranaf
    Commented Jan 20, 2014 at 10:04
  • Ok, the image is there. You probably won't see this with browser, but any RSS feed parser will definitely find it. Now, if you want to display the image within the description, then you need to revert to your old code, and put back the line <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); }?> just after the tag <description>, before <?php $object = new WPSEO_Frontend();.
    – Shazzad
    Commented Jan 20, 2014 at 10:08


Your Answer

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

Browse other questions tagged or ask your own question.