Skip Navigation

[Resolved] How can I add taxonomy terms to the Custom Post Type RSS feed?

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 16 replies, has 2 voices.

Last updated by PaulS4783 5 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#1202099

I am using the WP feed to create an automated newsletter mail out in MailChimp.
Currently it posts a new newsletter each month containing a list of any new posts during that period.
The ToolSet custom post type is "Events".

I can get it to return the post title as a link and the post body excerpt. I would like to be able to show the custom taxonomy (Event Type / Location) terms for each post as well.

How can I expose the custom post taxonomy within hidden link RSS
in order to add it in a newsletter?

#1202412

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You already have a feed for your event custom post type, but you want to customise what is included in the feed so that the assigned custom taxonomy terms are included in the field, right?

I'm no expert in this, but it seems to me you have two options.

One is to create a custom template for this feed, which is discussed in the WP documentation here: https://codex.wordpress.org/Customizing_Feeds

Easier, probably, would be to use the feed hooks to append content for each item in the feed, for example using the rss2_item hook, as described here: https://codex.wordpress.org/Plugin_API/Action_Reference/rss2_item

You can get the terms assigned to the current post (you have the global $post object available) and echo these out into the feed.

#1202758

Thanks. Two questions about the second option.

<?php
add_action('rss2_item', 'add_my_custom_field_node');

function add_my_custom_field_node() {
	global $post;
	$metaValue = get_post_meta($post->ID, 'my-custom-field', true);
	if(!empty($metaValue)):
		echo("<my-custom-field>{$metaValue}</my-custom-field>");
	endif;
}
?>

1. If I want to add a custom field to the RSS feed, "my-custom-field" is the value of the field slug, correct?
2. If I want the taxonomy term for the item (e.g. event type, location) how would this code be different?

#1203331

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Paul

Sorry, the forum is very busy at the moment, and I just did not get chance to look at this today, I will as soon as possible.

#1203852

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Paul

1. Yes, you need to replace the my-custom-field of the get_post_meta call with the slug of your custom field. Remember that Types fields use a wpcf- prefix, e.g. 'wpcf-my-custom-field'. I don't know, but I doubt that the my-custom-field tags that are echoed need to be the slug as such, the opening and closing tags probably just need to match and be distinct from other such tags.

2. You can use a WordPress function such as get_the_terms to retrieve the terms of a particular taxonomy attached to the post, passing the global $post object, see https://developer.wordpress.org/reference/functions/get_the_terms/

That returns an array, so you will need to use implode() to turn that into a string to be echoed: hidden link

#1205346

I've tried this:

add_action('rss2_item', 'add_my_custom_field_node');
function add_my_custom_field_node() {
    global $post;
    $metaValue = get_post_meta($post->ID, 'wpcf-start-date', true);
    if(!empty($metaValue)):
        echo("<start-date>{$metaValue}</start-date>");
    endif;
}

But it doesn't output to the custom post type feed
hidden link

Is global $post correct?
Even for custom post types?

#1205546

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-02-25 at 12.17.13.png

It looks like that code is working, <start-date> tags are being added to the feed.

#1205606

Ah. OK!
Maybe there was a delay.

Last question: how can i get those dates in human readable form?
Are they unicode?

#1205641

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The raw dates are unix timestamps.

You can output them in whatever format you want using the PHP date function, e.g.

add_action('rss2_item', 'add_my_custom_field_node');
function add_my_custom_field_node() {
    global $post;
    $metaValue = get_post_meta($post->ID, 'wpcf-start-date', true);
    if(!empty($metaValue)):
        $metaDate = date("d M Y", $metaValue);
        echo("<start-date>{$metaDate}</start-date>");
    endif;
}
#1205917

I've discovered that WordPress uses a 12 hour cache for RSS feeds.
Which would explain why I'm not seeing the changes after implementing your code above.
hidden link

I've tried this code snippet but it doesnt do anything.

function return_cache_time( $seconds )
{
// change the default feed cache recreation period to 60 secs
return (int) 60;
}
 
//set feed cache duration
add_filter( 'wp_feed_cache_transient_lifetime', 'return_cache_time');

So I will have to wait up to 12 hours to see if the date formating code snippet above works.
But I have every confidence that it should.
Will update this ticket later in the day.

#1206665

Its over 24 hours now and the RSS feed is still showing the dates in UNIX format, not human readable format.
hidden link

Any ideas why?

#1206849

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Can you publish a new post and check what appears in the feed for that post?

If the new post outputs the date correctly then you will know that the issue is related to caching, rather than the code you added.

What do you see?

#1207357

Yeah, I added a new post "Test New Event for Feeds" which shows up in the RSS feed but still in UNIX format.
hidden link

#1207429

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-02-28 at 10.06.33.png

Are you sure you updated the code?

I just tried the same code on my test site and the date in the feed is the human readable date in the format specified in the code (screenshot).

#1207448

You are right.
I think I found an omission.