{"id":310,"date":"2020-09-01T16:09:10","date_gmt":"2020-09-01T16:09:10","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/how-do-i-import-an-rss-feed-as-wordpress-posts-properlywithout-duplicates\/"},"modified":"2024-11-10T04:53:46","modified_gmt":"2024-11-10T04:53:46","slug":"how-do-i-import-an-rss-feed-as-wordpress-posts-properlywithout-duplicates","status":"publish","type":"post","link":"https:\/\/demo.wpartisan.net\/developmentsite\/how-do-i-import-an-rss-feed-as-wordpress-posts-properlywithout-duplicates\/","title":{"rendered":"How do I Import an RSS feed as WordPress posts properly(without duplicates)?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We can import an RSS feed to WordPress as posts or other Custom Post Type( CTP ) posts using some WordPress and PHP functions like wp_schedule_event(), wp_insert_post(), simplexml_load_file() etc., like the below:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_action('import_rss_feed', 'func_import_rss_feed');\n\n\/\/ The action will trigger when someone visits your WordPress site\nfunction func_run_schedule_event() {\n    if ( !wp_next_scheduled( 'import_rss_feed' ) ) {\n        wp_schedule_event( current_time( 'timestamp' ), 'daily', 'import_rss_feed');\n    }\n}\nadd_action('wp', 'func_run_schedule_event');\n\nfunction func_import_rss_feed() {\n    $url = 'https:\/\/wordpress.org\/?feed=rss';\n\t$xml = simplexml_load_file( $url );\n\t\n\tforeach( $xml->channel->item as $item ) {\n\t\t$title = (string)$item->title;\n\t\t$link = (string)$item->link;\n\t\t$post_content = (string)$item->description;\n\t\t$args = array(\n\t\t\t'fields' => 'ids',\n\t\t\t'post_type'   => 'post',\n\t\t\t'post_status' => 'publish',\n\t\t\t'meta_query'  => array(\n\t\t\t\tarray(\n\t\t\t\t\t'key' => '_link',\n\t\t\t\t\t'value' => $link\n\t\t\t\t)\n\t\t\t)\n\t\t);\n\t\t$pre_posts = new WP_Query( $args );\n\t\t\n\t\tif( empty( $pre_posts->posts ) ) {\n\n\t\t\t$post_data = array(\n\t\t\t\t'post_title' => $title,\n\t\t\t\t'post_type' => 'post',\n\t\t\t\t'post_content' => $post_content,\n\t\t\t\t'post_status' => 'publish',\n\t\t\t\t'meta_input' => array(\n\t\t\t\t\t'_link' => $link,\n\t\t\t\t)\n\t\t\t);\n\t\t\t$post_id = wp_insert_post( $post_data );\n\t\t}\n\t\t\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If we want, we can also assign all the imported posts to a specific category when inserting posts passing the <code>post_category<\/code> parameter in wp_insert_post() function. Please check <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_insert_post\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for more details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Let&#8217;s describe the above code step by step:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use of wp_schedule_event() function:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">\/\/ The action will trigger when someone visits your WordPress site\nfunction func_run_schedule_event() {\n    if ( !wp_next_scheduled( 'import_rss_feed' ) ) {\n        wp_schedule_event( current_time( 'timestamp' ), 'daily', 'import_rss_feed');\n    }\n}\nadd_action('wp', 'func_run_schedule_event');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In func_run_schedule_event(), first, we need to check the next timestamp for an event( which means retrieves the next time the event will occur ). If not found, then wp_schedule_event() will be called( which means schedules a hook that will be triggered by WordPress at the specified interval ). wp_schedule_event() expect 4 parameters( $timestamp, $recurrence, $hook, $args ). Here first 3 parameters are required, and the last 1 is optional. For more details, please check <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_schedule_event\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use of simplexml_load_file() and wp_insert_post() functions:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now the below action will trigger when someone visits your WordPress site if the scheduled time has passed.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_action('import_rss_feed', 'func_import_rss_feed');\n\nfunction func_import_rss_feed() {\n    $url = 'https:\/\/wordpress.org\/?feed=rss';\n\t$xml = simplexml_load_file( $url );\n\t\n\tforeach( $xml->channel->item as $item ) {\n\t\t$title = (string)$item->title;\n\t\t$link = (string)$item->link;\n\t\t$post_content = (string)$item->description;\n\t\t$args = array(\n\t\t\t'fields' => 'ids',\n\t\t\t'post_type'   => 'post',\n\t\t\t'post_status' => 'publish',\n\t\t\t'meta_query'  => array(\n\t\t\t\tarray(\n\t\t\t\t\t'key' => '_link',\n\t\t\t\t\t'value' => $link\n\t\t\t\t)\n\t\t\t)\n\t\t);\n\t\t$pre_posts = new WP_Query( $args );\n\t\t\n\t\tif( empty( $pre_posts->posts ) ) {\n\n\t\t\t$post_data = array(\n\t\t\t\t'post_title' => $title,\n\t\t\t\t'post_type' => 'post',\n\t\t\t\t'post_content' => $post_content,\n\t\t\t\t'post_status' => 'publish',\n\t\t\t\t'meta_input' => array(\n\t\t\t\t\t'_link' => $link,\n\t\t\t\t)\n\t\t\t);\n\t\t\t$post_id = wp_insert_post( $post_data );\n\t\t}\n\t\t\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this callback function( func_import_rss_feed() ) I used 2 function name simplexml_load_file() and wp_insert_post(). The simplexml_load_file() function is used to get feed data from remote URLs, and the wp_insert_post() function used to insert those feed data programmatically in WordPress as a blog post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before starting to import an RSS feed as WordPress posts, you need to know about some WordPress and PHP functions like wp_schedule_event(), wp_insert_post(), simplexml_load_file() etc. Let&#8217;s describe its details:<\/p>\n","protected":false},"author":1,"featured_media":2309,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15,18],"tags":[29,30,31,32,33],"class_list":["post-310","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-wordpress","tag-cron-schedules","tag-import-rss-feed","tag-simplexml-load-file","tag-wp-insert-post","tag-wp-schedule-event"],"uagb_featured_image_src":{"full":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress.png",966,596,false],"thumbnail":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress-150x150.png",150,150,true],"medium":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress-300x185.png",300,185,true],"medium_large":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress-768x474.png",768,474,true],"large":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress.png",966,596,false],"1536x1536":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress.png",966,596,false],"2048x2048":["https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/09\/Import-RSS-feed-to-WordPress.png",966,596,false]},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"Before starting to import an RSS feed as WordPress posts, you need to know about some WordPress and PHP functions like wp_schedule_event(), wp_insert_post(), simplexml_load_file() etc. Let's describe its details:","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":3,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":2303,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/310\/revisions\/2303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media\/2309"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}