0

How do I access the channel data when using fetch_feed()? I want to use the channel title.

TIA

Further info:

A typical RSS feed (taken from wikipedia)

  <?xml version="1.0" encoding="UTF-8" ?>
  <rss version="2.0">
 <channel>
 <title>RSS Title</title>
 <description>This is an example of an RSS feed</description>
 <link>http://www.example.com/main.html</link>
 <copyright>2020 Example.com All rights reserved</copyright>
 <lastBuildDate>Mon, 6 September 2010 00:01:00 +0000</lastBuildDate>
 <pubDate>Sun, 6 September 2009 16:20:00 +0000</pubDate>
 <ttl>1800</ttl>

 <item>
  <title>Example entry</title>
  <description>Here is some text containing an interesting description.</description>
  <link>http://www.example.com/blog/post/1</link>
  <guid isPermaLink="false">7bd204c6-1655-4c27-aeee-53f933c5395f</guid>
  <pubDate>Sun, 6 September 2009 16:20:00 +0000</pubDate>
 </item>

</channel>
</rss>

I wish to extract and use the channel title instead of the item title (because the mastodon feed doesn't use them currently).

I can access the data in <item> but accessing parts of <channel> I can't work out.

Does that help?

5
  • can you describe what you meant by channel? Is this a standard RSS feed of posts/entries or something else? I'm not seeing code in your question that can be modified either
    – Tom J Nowell
    Dec 1, 2022 at 10:20
  • Added information above.
    – skribe
    Dec 1, 2022 at 11:35
  • According to the documentation fetch_feed() returns a SimplePie object, and a very quick search for "SimplePie channel" returns simplepie.org/wiki/reference/simplepie/get_channel_tags. Have you tried that? Dec 1, 2022 at 12:08
  • ooooh you're trying to display mastodon toots and want to grab the profile name from the main title
    – Tom J Nowell
    Dec 1, 2022 at 12:25
  • 1
    I expect that by the time I work out the vagaries of developing a wordpress plugin, Mastodon will have fixed their feed mechanics. But it's a good learning process. I just wish the documentation was more helpful. Cheers!
    – skribe
    Dec 2, 2022 at 2:50

1 Answer 1

0

The official WP docs say it returns a SimplePie object and the very first example in the SimplePie documentation is:

// Single feed
$feed = new SimplePie('http://simplepie.org/blog/feed/', $_SERVER['DOCUMENT_ROOT'] . '/cache');
echo $feed->get_title();

https://simplepie.org/wiki/reference/simplepie/start

$feed is the same type of object as the return value of fetch_feed:

Return

SimplePie|WP_Error SimplePie object on success or WP_Error object on failure.

https://developer.wordpress.org/reference/functions/fetch_feed/

Just make sure to call is_wp_error to check for failures

1
  • Thank you very much.
    – skribe
    Dec 2, 2022 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.