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

Here is the complete code:

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 );
		}
		
	}
}

Lets describe above code step by step:

// 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(), first we need to check next timestamp for an event( means Retrieve the next timestamp for an event ). If not found, then wp_schedule_event() will called( Schedules a hook which 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 last 1 is optional. For more details, please check this: https://developer.wordpress.org/reference/functions/wp_schedule_event/

Now the below action will trigger when someone visits your WordPress site if the scheduled time has 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 this hook function( func_import_rss_feed() ) i use 2 function name simplexml_load_file() and wp_insert_post(). simplexml_load_file() function used for to get feed data from remote url and wp_insert_post() function used to insert those feed data programmatically in WordPress as a blog post.

Leave a Reply

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