17

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

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

  • 1
    so I'd need to add an item to the post type check for each meta key (probably around 10) then call it into the template? You can't just call get_post_meta in the meta value area with the post id? – curtismchale Nov 9 '10 at 5:29
  • 2
    Nice answer! – MikeSchinkel Nov 9 '10 at 7:07
  • @curtismchale, I just included the simplest answer. If you're pulling in that many fields, I would probably go with something formatted more like @mikeschinkel's answer. – prettyboymp Nov 9 '10 at 13:05
19

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.

7

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');
  • For those showing up here: Jetpack ads the xmlns:geo and xmlns:georss namespaces. No need to add these if you're using Jetpack. – MastaBaba Aug 29 '18 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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