如何将RSS提要导入为WordPress帖子而不复制?

时间:2012-10-17 作者:Amit Erandole

我有一个XML提要url,现在我正在使用此代码从我的函数中的rss提要创建新的自定义帖子类型。php文件:

    /*
| -------------------------------------------------------------------
| Schedule and update fashion news with the news rss feed
| -------------------------------------------------------------------
| 
| */

if (!wp_next_scheduled(\'update_feed\'))
    wp_schedule_event(current_time(\'timestamp\'), \'hourly\', \'update_feed\');

add_action(\'update_feed\', \'update_fashion_news\');

function update_fashion_news() {
    // retrieve the previous date from database
        $time = get_option(\'latestpostdate\');

        //read the feed
        if(function_exists(\'fetch_feed\')){
            $uri = \'http://www.sitename.com/feed.xml\';
            $feed = fetch_feed($uri);
        }

        if($feed) {

            foreach ($feed->get_items() as $item){
                $titlepost = $item->get_title();
                $content = $item->get_content();
                $description = $item->get_description();
                $itemdate = $item->get_date();
                $media_group = $item->get_item_tags(\'\', \'enclosure\');
                $img = $media_group[0][\'attribs\'][\'\'][\'url\'];
                $width = $media_group[0][\'attribs\'][\'\'][\'width\'];           
                // $latestItemDate = $feed->get_item()->get_date();


                // if the date is < than the date we have in database, get out of the loop
                if( $itemdate <= $time) break;


                // prepare values for inserting

                $post_information = array(
                    \'post_title\' => $titlepost,
                    \'post_content\' => $description,
                    \'post_type\' => \'fashionnews\',
                    \'post_status\' => \'publish\',
                    \'post_date\' => date(\'Y-m-d H:i:s\')
                );

                wp_insert_post( $post_information );    

            }
        }
        // update the new date in database to the date of the first item in the loop        
        update_option( \'latestpostdate\', $feed->get_item()->get_date() );
}
[更新:已更新上面的代码,以从simple\\u xml移动到使用simplepie]

[更新2:按照下面@Mridul的建议,移动代码并将其包装在WP计划事件中]

我仍在检查我的代码在下一批新闻更新到达时是否有效。有人认为这个代码会因为某种原因而不起作用吗?

1 个回复
最合适的回答,由SO网友:Mridul Aggarwal 整理而成

RSS提要以固定顺序(从最新到最旧)包含所有项目。在这种情况下,您可以保存日期(&A);您上次创建帖子的时间(可选)(&;再次阅读提要时,可以查看以前保存的时间,以了解提要中的哪些帖子是新的&;插入它们,然后再次更新时间

您感兴趣的功能有update_option, get_option &;wp_insert_post. 你可以在wordpress codex中找到它们的参考资料,只需谷歌一下。水流会是这样的

// retrieve the previous date from database
$time = get_option(\'mylastfeeddate\');

// include the code to read the xml file here &
// then the foreach loop as you did in the question
foreach($items as $item) {

    // if the date is < than the date we have in database, get out of the loop
    if( $item->pubDate < $time) break;

    // assign the values in the format of wp_insert_post()
    $out = array();

    // insert the post
    $post_id = wp_insert_post( $out );

}

// update the new date in database to the date of the first item in the loop
update_option( \'mylastfeeddate\', $items[0]->pubDate );

UPDATE

要使代码在某个固定时间段后执行,请使用wordpress cron函数,如下所示

if (!wp_next_scheduled(\'update_feed\'))
    wp_schedule_event(current_time(\'timestamp\'), \'hourly\', \'update_feed\');

add_action(\'update_feed\', \'function_name\');
function function_name() {
    // here goes all the code to read the feed
}
更改hourly 你喜欢这里的时间是抄本参考http://codex.wordpress.org/Function_Reference/wp_schedule_event

用于添加自定义时间范围

add_filter(\'cron_schedules\', \'new_cron_schedules\');
function new_cron_schedules($schedules) {
    array_merge($schedules, array(\'two_hourly\' => array( \'interval\' => 7200, \'display\' => __(\'Twice Hourly\') )));
}
在此过滤器之后,例如,您可以将上面的“hourly”替换为“two\\u hourly”

结束

相关推荐

wp3 custom post types rss

从我在网上发现的情况来看,获取自定义帖子类型的rss提要非常简单:http://mywebsite.com/feed/?post_type=custom_type 我试过了,但没有成功,因为它只返回默认提要。但是我可以在网站上使用它http://mywebsite.com/?post_type=custom_type 那么,为什么它适用于html而不适用于rss提要呢?请告知。**编辑**最后我把它添加到了我的主题函数中。php:function myfeed_request($qv)