Add Custom Fields to Custom Post Type RSS

The question:

I’d like to add custom fields in my custom post type to the RSS feed for that post type located at http://example.com/feed/?post_type=my_custom_post_type

I’ve seen info on doing this for the regular feed but nothing for how to rewrite the custom post type feed.

I’ll need to add 10 – 15 items to the feed (1st act, 2nd act, 3rd act, price, purchase link…)

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

function add_custom_fields_to_rss() {
    if(get_post_type() == 'my_custom_post_type' && $my_meta_value = get_post_meta(get_the_ID(), 'my_meta_key', true)) {
        ?>
        <my_meta_value><?php echo $my_meta_value ?></my_meta_value>
        <?php
    }
}
add_action('rss2_item', 'add_custom_fields_to_rss');

You should be able to substitute and any other meta values you need to add to the feed.

Method 2

Hi @curtismchale:

Piggybacking off @prettyboymp’s excellent answer,with my spin on it, here’s how you can do multiple custom fields (I did 3, you can do more):

add_action('rss2_item', 'yoursite_rss2_item');
function yoursite_rss2_item() {
  if (get_post_type()=='my_custom_post_type') {
    $fields = array( 'field1', 'field2', 'field3' );
    $post_id = get_the_ID();
    foreach($fields as $field)
      if ($value = get_post_meta($post_id,$field,true))
        echo "<{$field}>{$value}</{$field}>n";
  }
}

P.S. Be sure to give @prettyboymp props because I didn’t know how to do this prior to his answer. I’m just answering too because I’m not sure how long it will be before he returns so I decided to give you an answer in the mean time.

Method 3

THANK YOU THANK YOU for this excellent piece of information.

I wanted to extend what the other two have written already…
For this to validate, you must have a custom namespace.
Here’s how you do that:

/* IN ORDER TO VALIDATE you must add namespace   */
add_action('rss2_ns', 'my_rss2_ns');
function my_rss2_ns(){
    echo 'xmlns:mycustomfields="'.  get_bloginfo('wpurl').'"'."n";
}

And then prefix the field name item with the custom namespace
In this example, I’ve used “mycustomfields”
See below:

/*  add elements    */
add_action('rss2_item', 'yoursite_rss2_item');
function yoursite_rss2_item() {
  if (get_post_type()=='my_custom_post_type') {
    $fields = array( 'field1', 'field2', 'field3' );
    $post_id = get_the_ID();
    foreach($fields as $field)
      if ($value = get_post_meta($post_id,$field,true))
        echo "<mycustomfields:{$field}>{$value}</mycustomfields:{$field}>n";
  }
}

On a side note you can use an action to hook into any of the 3

    rss2_ns : to add a specific namespace
            add_action('rss2_ns', 'my_rss2_ns');

    rss2_head : to add tags in the feed header
            add_action('rss2_head', 'my_rss2_head');

    rss2_item : to add tags in each feed items
            add_action('rss2_item', 'my_rss2_item');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment