Mwesigwa
Mwesigwa

Reputation: 51

Insert RSS Feeds as WP posts

how do I retrieve a feed & insert it using wp_insert_post(); Am so much interested if the function can be able to pick 2 feeds each feed from a unique source.

Upvotes: 1

Views: 717

Answers (1)

Simon Pollard
Simon Pollard

Reputation: 2588

If you are able to code PHP its easy to loop through an RSS feed and insert the data into WordPress. I usually create a new post type (though of course you can use the default 'post' type). In the example below I am using a post type I made called 'article'.

There are many ways to loop through an RSS feed, here is what I use:

$rss = new DOMDocument();
$rss->load($rss_url);

// Loop through each item in the feed
foreach ($rss->getElementsByTagName('item') as $node) {
  // Code goes here

  // Example to get a value
  // Define a namespace
  $ns = 'http://purl.org/rss/1.0/modules/content/';
  $content = $node->getElementsByTagNameNS($ns, 'encoded');
  $content = $content->item(0)->nodeValue;
}

In the loop through your RSS feed get the data and save as variables, then run the following:

$new_article = array(
    'post_title'    => $title,
    'post_content'  => $content,
    'post_excerpt'  => $description,
    'post_type'     => 'article',
    'post_date'     => date('Y-m-d H:i:s',strtotime($date)),
    'post_author'   => 1,
    'post_status'   => 'publish'
);
wp_insert_post( $new_article , true );

Adjust as needed to fit your needs.

Hope that helps, it will give you a lot more control than a plugin.

Upvotes: 2

Related Questions