themissionmars
themissionmars

Reputation: 117

How to remove duplicate posts from Simplepie (Multiple) feeds

I have multiple branch sites that feed their RSS news feeds to one main site using Simplepie. This works great. The only problem is, sometimes multiple branches post the same news article and that in turn displays multiples news articles on the main site. How would I go about removing the duplicates?

<?php
require_once(ABSPATH .'php/simplepie.inc');
$feed = new SimplePie();

$feed->set_feed_url(array(

'http://www.branch1.com/feed/',
    'http://www.branch2.com/feed/',
    'http://www.branch3.com/feed/',
    'http://www.branch4.com/feed/',

));

$feed->set_favicon_handler('handler_image.php');
$feed->init();
$feed->handle_content_type();


foreach ($feed->get_items() as $property):

    // I want this to be unique
    echo $property->get_title();

endforeach;
?>

I have already tried. With no luck.

foreach (array_unique($unique) as $property):

I also tried to do a second foreach looking for any matching titles. And only displaying the ones that are have the number 1 next to them or the first match... But It kept giving me the amount of matches instead of:

1.Match0 1.Match1 2.Match1 3.Match1 1.Match2 2.Match2 ect ect...

foreach ($feed->get_items() as $property):

 $t = $property->get_title();
 $match = 0;

foreach ($feed->get_items() as $property2): 
  $t2 = $property2->get_title();

  if ($t == $t2){
   $match++;
   //echo $match;
      }

    if ($match <= 2){echo "$match. $t <br/> ";}

    endforeach;

endforeach;

Upvotes: 2

Views: 1734

Answers (1)

Teno
Teno

Reputation: 2632

Try putting the fetched items in another array with the key of the title so that duplicated titles will be overwritten in the array. Then you pull back the contents from the array.

$arrFeedStack = array();
foreach ($feed->get_items() as $property):
    $arrFeedStack[$property->get_title()] = $property->get_description();
endforeach;

foreach ($arrFeedStack as $item) {
    echo $item . <br />;
}

Upvotes: 4

Related Questions