3

I've got a google news feed I display in my WordPress site, using the following code:

$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(20); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
foreach ($items as $item) : 
    echo $item->get_description(); 
endforeach;

Problem is, certain individual articles I need to filter out. Google news items have guid tags. Given the guid of the item, how can I tell SimplePie to ignore the given item?

Thanks-

Yarin
  • 173,523
  • 149
  • 402
  • 512

1 Answers1

3

SimplePie does not have built-in filtering functions (yet). However, you can selectively show only the items you wish:

$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(20); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
$ignoreGUIDs = array("http://example.com/feed?id=1", "http://example.com/feed?id=2");
foreach ($items as $item) : 
    if(!in_array($item->get_id(false), $ignoreGUIDs)){
        echo $item->get_description();
    }
endforeach;

The get_id() method returns an array of the item's <guid>, <link>, and <title> tags, each of which the in_array() clause then searches for a match of each of your $ignoreGUIDs. If there are no matches, it means the item's GUID is not in your exlusion list and so the item is shown (by echo).

Jani Uusitalo
  • 226
  • 1
  • 9
rzetterberg
  • 10,146
  • 4
  • 44
  • 54
  • @Ancide- Surprising that there's no filtering, but your solution is the best option after that. Thanks for the get_id reference... – Yarin May 19 '11 at 15:09