How do I Import an RSS feed as WordPress posts properly(without duplicates)?

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top