How to filter / block RSS feed items using SimplePie

I have a Google feed that I display on 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;

The problem is some of the individual articles that I need to filter out. Google News has landmarks. Given an element directive, how can I tell SimplePie to ignore a given element?

Thank -

+3
source share
1 answer

SimplePie has no built-in filtering features (yet). However, you can selectively display only those items that you want:

$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;

get_id() <guid>, <link> <title>, in_array() $ignoreGUIDs. , , GUID , (echo).

+3

All Articles