Skip Navigation

[Resolved] Creating RSS feed for custom post type

This support ticket is created 5 years, 1 month 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.

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)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1353631

A client needs an RSS feed for one custom post type - the feed works by default: hidden link

However, the author of these articles is not the WordPress author, but the author stored in a many-many relationship. Authors are stored in the 'people' custom post type and the 'Publication Author' post type stores the relationship.

To display in the front end a view shows the authors of each article: hidden link

Are you able to assist with the code to update the RSS feed - replacing the WordPress author of each article with the authors from the relationship? I assume this is possible by using a function to call an array and replace the field value of author in the RSS feed?

#1353911

Nigel
Supporter

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

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

Hi Ian

I can't help you with the details of customising feeds, though there are lots of online tutorials about that.

What I can help you with is the part about using the Toolset API to get the related posts.

You'll be editing the RSS template, identifying where the post author is output, and replacing that with the title of the related post.

The code you need to add to the template where you want to output the "author" would be something like this:

$post_id = get_the_ID();
$relationship = 'relationship-slug';

$connected_posts = toolset_get_related_posts( 
    $post_id,
    $relationship,
    array(
        'query_by_role'     => 'parent', // starting from
        'role_to_return'    => 'child', 
        'return'        =>  'post_object'
    )
);

foreach ($connected_posts as $connected_post) {
    echo $connected_post->post_title;
}
#1354765

Thanks Nigel - trying it out and will report back later. best, Ian

#1354947

Nigel
Supporter

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

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

OK, keep me posted.

#1355579

Hi Nigel

Thanks very much for this - it's working! Just two issues:
- the first was that our parent/child relationship is the other way around, so I swapped that around and it works
- but the list of authors needs to be separated by a comma. I have added this to the echo line, but it now has a comma on the last result also - is there a more elegant way to remove that comma from the last (or a single) result?

many thanks for your assistance - I'm fumbling around in the dark a bit with PHP unfortunately.
Ian

<?php
/**
* Template Name: Custom RSS Template - saiiawef
*/
$postCount = 10; // The number of posts to show in the feed
$postType = 'publication'; // post type to display in the feed
$posts = query_posts( array( 'post_type' => $postType, 'showposts' => $postCount ) );
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
xmlns:content="hidden link"
xmlns:wfw="hidden link"
xmlns:dc="hidden link"
xmlns:atom="hidden link"
xmlns:sy="hidden link"
xmlns:slash="hidden link"
<?php do_action('rss2_ns'); ?>>
<channel>
<title><?php bloginfo_rss('name'); ?> - Feed</title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss('description') ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language>en</language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
<?php do_action('rss2_head'); ?>
<?php while(have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><?php $post_id = get_the_ID();
$relationship = 'person-analysis-opinion';
$connected_posts = toolset_get_related_posts(
$post_id,
$relationship,
array(
'query_by_role' => 'child', // starting from
'role_to_return' => 'parent',
'return' => 'post_object'
)
);

foreach ($connected_posts as $connected_post) {
echo $connected_post->post_title.',';
}
?></dc:creator>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>

#1355629

Nigel
Supporter

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

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

Hi Ian

You could try this:

$post_id = get_the_ID();
$relationship = 'person-analysis-opinion';
$connected_posts = toolset_get_related_posts(
    $post_id,
    $relationship,
    array(
        'query_by_role' => 'child', // starting from
        'role_to_return' => 'parent',
        'return' => 'post_object',
    )
);

$titles = wp_list_pluck( $connected_posts, 'post_title' );
$list_of_titles = implode( ', ', $titles );

echo $list_of_titles;

We get back an array of connected post objects. We can use wp_list_pluck to extract just the titles in an array.

Then we can use implode to generate the comma-separated list of titles.