1
votes

I have a friend who writes to a couple of different blogs and wants to pull stories from one that isn't so popular, onto his main blog with an rss feed and display an image from it too (since rss feeds have images, sometimes).

Displaying the rss feeds shouldn't be too hard, it's making them a custom post type that seems more difficult to me.

If anyone has any ideas, shoot.

EDIT:- Does anyone know how to get an external rss feed to appear as a custom post type in wordpress?

4

4 Answers

4
votes

A simple way may be to use Wordpress' own fetch_feed function: http://codex.wordpress.org/Function_Reference/fetch_feed

A quick example (assuming you've already set up your custom post type):

function import_feed_items()
{
  $feed = fetch_feed('http://feeds.bbci.co.uk/news/uk/rss.xml');

  if( !is_wp_error($feed) )
  {

    if( $last_import = get_option('last_import') )
    {
      $last_import_time = $last_import;
    } else {
      $last_import_time = false;
    }

    $items = $feed->get_items();
    $latest_item_time = false;

    foreach ( $items as $item )
    {

      $item_date = $item->get_date('Y-m-d H:i:s');
      if( $last_import_time && ($last_import_time >= strtotime($item_date)) )
      {
        continue;
      }

      $post = array(
        'post_content'   => $item->get_content(),
        'post_date'      => $item_date,
        'post_title'     => $item->get_title(),
        'post_status'    => 'publish',
        'post_type'      => 'custom_post_type'
      );
      wp_insert_post($post);

      if( strtotime($item_date) > $latest_item_time )
      {
        $latest_item_time = strtotime($item_date);
      }

    }

    if( false !== $latest_item_time )
    {
      update_option('last_import', $latest_item_time);
    }

  }
  else
  {
    echo $feed->get_error_message();
  }
}
add_action('wp', 'import_feed_items');

If there is an image tag in the content you could use php's DomDocument class to grab the url and upload it to your server so you can set it as the featured image.

http://codex.wordpress.org/Function_Reference/wp_insert_attachment

http://codex.wordpress.org/Function_Reference/set_post_thumbnail

Edit

corrected the timestamp check. This updated example uses the 'wp' hook to run so you can see the results quicker. It would be preferable to set this as a cron task. See http://codex.wordpress.org/Function_Reference/wp_schedule_event

1
votes

The Feed to Post plugin is the perfect solution for importing multiple RSS items into your WordPress blog, you can even store them as posts or custom post types.

http://www.wprssaggregator.com/extension/feed-to-post/

0
votes

Unfortunately I don't know a way off hand, but have you looked at modifying a plugin? There are tons of content curation plugins around (feedwordpress, autoblog, etc.). You could probably find the wp_insert_post() line somewhere and modify it to include your custom post type/taxonomies.

EDIT Jumped into the plugin (feedwordpress) myself, and all the insert_post stuff is in syndicatedpost.class.php - the main wp_insert_post() on line 1538

EDIT if you fancy a giggle while reading throught that plugin code, you'll find many an instance of the f word... haha

0
votes

Hey why dont you try this http://wordpress.org/extend/plugins/display-latest-rss-feeds/ It would pull rss feeds from any account and display it to your blog. Unfortunately it wont display images just the rss feeds title and its permalink to the original blog but you can easily modify the source code if you want.