We can import an RSS feed into WordPress as standard posts or as Custom Post Type (CPT) entries by using WordPress and PHP functions such as wp_schedule_event(), wp_insert_post(), and simplexml_load_file(), as shown below:
add_action('import_rss_feed', 'func_import_rss_feed');
// The action will trigger when someone visits your WordPress site
function func_run_schedule_event() {
if ( !wp_next_scheduled( 'import_rss_feed' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'import_rss_feed');
}
}
add_action('wp', 'func_run_schedule_event');
function func_import_rss_feed() {
$url = 'https://wordpress.org/?feed=rss';
$xml = simplexml_load_file( $url );
foreach( $xml->channel->item as $item ) {
$title = (string)$item->title;
$link = (string)$item->link;
$post_content = (string)$item->description;
$args = array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_link',
'value' => $link
)
)
);
$pre_posts = new WP_Query( $args );
if( empty( $pre_posts->posts ) ) {
$post_data = array(
'post_title' => $title,
'post_type' => 'post',
'post_content' => $post_content,
'post_status' => 'publish',
'meta_input' => array(
'_link' => $link,
)
);
$post_id = wp_insert_post( $post_data );
}
}
}
If desired, we can assign all imported posts to a specific category by passing the post_category parameter to the wp_insert_post() function. For more details, please refer to the documentation.
Now, let’s go through the code step by step:
Use of wp_schedule_event() function:
// The action will trigger when someone visits your WordPress site
function func_run_schedule_event() {
if ( !wp_next_scheduled( 'import_rss_feed' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'import_rss_feed');
}
}
add_action('wp', 'func_run_schedule_event');
In func_run_schedule_event(), we first need to check the next timestamp for the event (which retrieves the next scheduled occurrence). If no timestamp is found, then wp_schedule_event() will be called, which schedules a hook to be triggered by WordPress at the specified interval.
The wp_schedule_event() function expects four parameters: $timestamp, $recurrence, $hook, and $args. The first three parameters are required, while the last one is optional. For more details, please refer to the documentation.
Use of simplexml_load_file() and wp_insert_post() functions:
The following action will be triggered when someone visits your WordPress site, provided the scheduled time has already passed.
add_action('import_rss_feed', 'func_import_rss_feed');
function func_import_rss_feed() {
$url = 'https://wordpress.org/?feed=rss';
$xml = simplexml_load_file( $url );
foreach( $xml->channel->item as $item ) {
$title = (string)$item->title;
$link = (string)$item->link;
$post_content = (string)$item->description;
$args = array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_link',
'value' => $link
)
)
);
$pre_posts = new WP_Query( $args );
if( empty( $pre_posts->posts ) ) {
$post_data = array(
'post_title' => $title,
'post_type' => 'post',
'post_content' => $post_content,
'post_status' => 'publish',
'meta_input' => array(
'_link' => $link,
)
);
$post_id = wp_insert_post( $post_data );
}
}
}
In the callback function func_import_rss_feed(), I used two functions: simplexml_load_file() and wp_insert_post(). The simplexml_load_file() function retrieves feed data from remote URLs, while the wp_insert_post() function inserts that feed data programmatically into WordPress as blog posts.
Plugins to Enhance Your WooCommerce Store
Carefully selected tools to help you customize, manage, and optimize your WooCommerce experience.
Woo Parent Category Selector
- Automatically assign parent categories when child categories are selected
- Prevent accidental removal of required parent categories
- Automatically repairs missing parent category assignments during product save
- Bulk repair existing WooCommerce product category structures
- Keeps product category structures organized and consistent
Product Catalog Mode For WooCommerce
- Turn your WooCommerce store into catalog mode instantly
- Hide Add to Cart buttons and product prices conditionally
- Apply rules by user role, country, or scheduled time periods
- Display custom messages and inquiry options for products
- Control visibility settings globally or per product
Remove Add to Cart Button for WooCommerce
- Remove Add to Cart buttons from shop and product pages
- Hide buttons conditionally for selected products or categories
- Replace Add to Cart with custom text or messages
- Control button visibility by user role and product type
- Lightweight settings interface with easy customization
Remove Product Content for WooCommerce
- Hide product title, image, price, rating, and descriptions
- Remove tabs, meta information, SKU, tags, and related products
- Control visibility separately for shop and single product pages
- Customize WooCommerce layouts without coding
- Enable or disable content elements with simple settings
Really Simple XML and HTML Sitemap
- Generate both XML and HTML sitemaps automatically
- Display sitemaps anywhere using ShortCode support
- Include pages, posts, and custom post types
- Exclude selected pages or posts easily
- Supports WPML and Polylang multilingual plugins



