This is a technical post sharing a problem I solved today. Hopefully it’ll be useful to some folks :)
The Problem: WordPress RSS Importer Only Imports Categories
I’m currently working on a Drupal-to-WordPress migration. We’re automatically migrating their blog posts, so using an RSS feed—set up with a Drupal View—is the simplest way to do that.
One downside of this approach in this case is that RSS only supports categories and therefore the WordPress RSS Importer only imports Categories. That turned out to be not very hard to solve.
Quick Note: The PHP7 Version of the Plugin
The current version of the plugin (v0.2) does not support PHP7 at all. Therefore, if you’re on PHP7, you’ll want to use this version 0.3 on GitHub until the official plugin repository version gets an update.
The Solution: Importing RSS Categories as Tags
First I needed to find where the plugin imports the RSS categories and sets them up as WordPress Categories. That happens on Line 161 and 162 of the plugin file:
if (0 != count($categories))
wp_create_categories($categories, $post_id);
Importing Tags instead turned out to be slightly trickier than expected because there is no wp_create_tags()
function in WordPress. Because of that, I had to loop through each category to first create its tag and then assign the resulting term to the Post.
Here’s the code. Take it and completely replace the Lines 161-162 of the v0.3 plugin:
if ( 0 != count($categories) ) {
foreach( $categories as $index => $label ) {
$term = wp_create_tag( $label );
wp_set_object_terms( $post_id,array( intval( $term['term_taxonomy_id'] ) ),'post_tag',true );
}
}
BONUS! Adjusting the Imported Author
The RSS Importer also does a rather “dumb” thing and sets the imported post author to the user with the ID of 1. That happens even if there is no user with that ID! (And there shouldn’t be!) Doik!
To fix that issue, just update the $post_author
value of “1” on Line 136 to the ID of the user you want to use instead. It likely wouldn’t be too hard to read the “dc:creator” value out of the feed and map those to different WordPress user accounts, but I didn’t need to do that in this case.