Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How can I add a list of related posts by tag within my RSS feed? WordPress

  • SOLVED

In the RSS for my site, at the bottom of each entry I want an unordered list of related posts by tag. I tried this but it didn't work -



<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);

?>

Answers (8)

2009-12-09

Gilbert Pellegrom answers:

Hi. Here is a working and tested implementation. Simply put this code in your "functions.php" file.


function feed_related_posts($content)
{
global $post;
$showposts = 5;

if(is_feed()){
$tags = wp_get_post_tags($post->ID);
if($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>$showposts,
'caller_get_posts'=>1
);

$my_query = new WP_Query($args);
if($my_query->have_posts()) {
$content .= '<h3>Related Posts</h3>';
$content .= '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
$content .= '<li><a href="'. get_permalink() .'" title="Permanent Link to '. the_title_attribute('echo=0') .'">'. get_the_title() .'</a></li>';
}
$content .= '</ul>';
}
}
}
return $content;
}
add_filter('the_content','feed_related_posts');

2009-12-08

Jean-Baptiste Jung answers:

Partial solution, hope it helps you or another developer :

<?php
function insertRelatedPosts($content) {
// Code to get the related posts goes here
// see http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin
}
add_filter('the_excerpt_rss', 'insertRelatedPosts');
add_filter('the_content_rss', 'insertRelatedPosts');
?>

2009-12-08

Dan Davies answers:


<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>

2009-12-08

nvartolomei answers:

<?php
/*
* use this within the loop
*/

/*
* initialize $tags variable, although php will output notice
*/
$tags = NULL;

/*
* we need to get all tags of current posts separated by comma
*/
foreach(get_the_tags() as $tag) {
$tags .= $tag->name . ",";
}

/*
* we already have all tags listed in $tags variable,
* now we need to get 5 posts with that tags,
* I didn't set the limit, becouse by deafult it's 5
*/
$posts_to_operate_with = get_posts('tag=' . $tags);

/*
* we need to know how many posts we get
*/
$number_of_posts = count($posts_to_operate_with);

/*
* if we have posts then go next
*/
if ($number_of_posts > 1) {

if ($number_of_posts < 5) {

/*
* if number of posts is not equal to 5 then it will be the limit
*/
$limit = $number_of_posts;
}

/*
* open unordered list tag
*/
echo "<ul>";
for($i = 0; $i < $limit; $i++) :
$my_post = $posts_to_operate_with[$i];

/*
* display link for each post
*/
?>

<li><a href="<?php echo $my_post->guid; ?>"><?php echo $my_post->post_title; ?></a></li>

<?php endfor;

/*
* close unordered list tag
*/
echo "</ul>";
}
?>

2009-12-08

nvartolomei answers:

And yeah, I'm not a wp developer.

2009-12-08

Arun Balasubramanian answers:

Other than the above codes, Here is a simple plugin to check it out (if you want) "[[LINK href="http://tadhg.com/wp/wordpress-plugins/related-posts-by-tag-widget/"]]Related Posts by Tags[[/LINK]]" plugin, which simply can do any/all of the following..
1. Related posts by tags in a widget
2. Related posts by tags under a single post and
3. Related posts by tags under every post in Rss Feed

2009-12-09

whiking answers:

Im using the plugin (yarpp) in order to display related posts in my posts. However there is also an option to include this into your rssfeed, thru a simple checkbox.

There are some options that you can tweak to your liking, like on what to put xtra weight on, categories and/or tags, or perhaps date and so forth and so on.

Read more about the plug in over @ wordpress/extends [[LINK href="http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/"]][[/LINK]]

Good Luck
//w

2009-12-09

Adam Kayce answers:

Use the "Yet Another Related Posts Plugin" : http://mitcho.com/code/yarpp/ and http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/ .

Quick and easy.