Skip to content

Instantly share code, notes, and snippets.

@scotthansonde
Last active July 7, 2025 06:35
Show Gist options
  • Select an option

  • Save scotthansonde/3b0bf489d71a96b309fefcf241463e3f to your computer and use it in GitHub Desktop.

Select an option

Save scotthansonde/3b0bf489d71a96b309fefcf241463e3f to your computer and use it in GitHub Desktop.
One-off php script to import a linkblog RSS feed into WordPress, putting the <link> item into a custom field
<?php
// Load WordPress
require_once(dirname(__FILE__) . '/../../../../wp-load.php');
// URL to the RSS feed
$feed_url = 'https://dave.linkblog.org/'; // Replace with your actual feed URL
$rss = simplexml_load_file($feed_url);
if (! $rss) {
die('Could not load RSS feed.');
}
foreach ($rss->channel->item as $item) {
$title = (string) $item->{'source:markdown'} ?: strip_tags((string) $item->title);
$description = html_entity_decode(strip_tags((string) $item->description));
$external_url = (string) $item->link;
$pub_date = (string) $item->pubDate;
// Check if already imported by GUID
$guid = (string) $item->guid;
$existing = get_posts([
'meta_key' => 'imported_guid',
'meta_value' => $guid,
'post_type' => 'post',
'post_status' => 'any',
'numberposts' => 1,
]);
if ($existing) {
continue; // Skip if already imported
}
// Insert post
$post_id = wp_insert_post([
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s', strtotime($pub_date)),
'post_category' => [get_cat_ID('linkblog')],
]);
if (is_wp_error($post_id)) {
echo 'Error inserting post: ' . $post_id->get_error_message() . '<br>';
continue;
}
// Save external URL
update_post_meta($post_id, 'external_url', esc_url_raw($external_url));
// Save GUID so we don’t reimport
update_post_meta($post_id, 'imported_guid', $guid);
echo 'Imported: ' . esc_html($title) . '<br>';
}
echo 'Done.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment