Blog

Handling Drupal terms during a Drupal to WordPress migration

When migrating Drupal terms into WordPress, it’s important to understand exactly what terms are and how the two systems handle categorising information.

A primer on Drupal taxonomies

One of Drupal’s most powerful features is its ability to organise content with taxonomies. Unfortunately, the taxonomy system is also notorious as one of the trickiest things about Drupal for beginners to understand. You can find a more detailed explanation here but essentially, a taxonomy is the practice and science of classifying things. In content management terms, you would mostly use taxonomies to organise and categorise articles or posts.

Taxonomies in Drupal uses the concept of vocabularies and terms. Terms are just a list of words that describe a particular type of content. They’re grouped together into vocabularies, which can be thought of as ‘containers’ for a set of terms. Vocabularies may be assigned to any content type. Drupal allows you to arrange the terms within a vocabulary using a parent-and-child hierarchical structure or they can be a flat list, with each term being on the same level as the others.

You can have many vocabularies in Drupal, each containing any number of terms. Vocabulary names must be unique and you cannot have duplicate term names within a vocabulary. It’s possible, however, to have the same term name appear in different vocabularies. Fig. 1 shows an example Drupal taxonomy with three vocabularies, Music, Movies and Books. The Movies and Books vocabularies both have the term Sci-Fi.

An example of Drupal vocabularies and terms
Fig 1: Drupal vocabularies and terms

For more information about the Drupal taxonomy system, please see Organizing content with taxonomies.

WordPress categories and tags

WordPress’ system for organising content is simpler. You have the option of categories–which can be hierarchical–and tags which are flat, or non-hierarchical. In general, categories in WordPress are used as a way of broadly organising posts and tags are used for more detailed descriptions.

Unlike Drupal, where you can have many containers in the form of vocabularies, a standard WordPress installation offers one container for categories and one for tags. Also as standard, categories and tags can only be assigned to the WordPress post content type. A WordPress developer can extend this by creating custom content types with their own categories and tags.

Fig. 2 shows show you’d organise the Music, Movies and Books categorisation in WordPress.

WordPress categories and tags
Fig. 2: WordPress categories and tags

Migrating Drupal terms as WordPress categories and tags

When running a Drupal to WordPress migration, we need to map Drupal’s more complex multi-vocabulary taxonomy system into the simpler WordPress model of categories and tags. How we do this depends on how you want to organise your new WordPress site. For example, we can:

  • convert Drupal vocabulary names into WordPress categories and Drupal term names into WordPress tags;
  • convert Drupal terms into WordPress categories and sub-categories;
  • vocabularies and their associated terms.

It’s all up to you and we figure this out during the requirements gathering stage of the project. For many sites, converting Drupal vocabulary names into WordPress categories and Drupal term names into WordPress tags, as shown in Fig. 3, seems to be the most sensible option. The important thing to know is that the migration may require us to ‘collapse’ or combine your taxonomies.

Merging Drupal and WordPress taxonomies
Fig 3: Merging Drupal taxonomies into WordPress

Since WordPress doesn’t support duplicate category or tag names, another thing to consider is how to handle any duplicate Drupal terms. Normally, the easiest solution is to append a unique number so that you can filter them out post-migration. We can do some clever merging and re-assigning of terms to posts but frankly, it’s probably not worth incurring the extra fees. Unless you have a great number of duplicates, you can probably do the job yourself quite easily via the WordPress Dashboard controls.

Organising your categories and tags in WordPress

Now that we know what’s involved in converting Drupal’s taxonomy over to WordPress, the next obvious question would be, “What’s the best way to structure categories and tags in WordPress?” While I cannot prescribe exactly how you should organise your site, I can point you to this excellent article so you can decide for yourself: Categories vs Tags – SEO Best Practices for Sorting your Content. Generally you should only have a few categories, maybe five or ten in total. Any more and they can become unwieldy and difficult to manage. These categories will reflect the main themes of your site. Tags can then further describe the details of each post and link specific topics together. You can have any number of tags.

The chances are that you probably want to avoid any drastic changes to the site structure when migrating from Drupal to WordPress. A simple mapping of vocabularies to categories and terms to tags is usually the closest equivalent in WordPress.

Getting started with a Drupal to WordPress migration project

A Drupal to WordPress migration can sound daunting, especially if you have a large or long-established site. From my experience, a migration project is actually not very difficult in terms of technical challenge. It can, however, be time-consuming, tedious and sometimes finicky with the data mappings. It’s my job to make the process easy for you.

To help with this, I write custom scripts to automate the bulk of your content migration. Once we have most of the data transferred, we can refine different aspects of the new WordPress site until you feel the job is done. These scripts save time by allowing us to repeatedly run the migration steps after building-in layers of improvements.

The thing to remember is that it’s often unnecessary to make an exact copy of your Drupal site in WordPress. We only need to prioritise the highest value content and functionality. Following the Pareto principle, or the 80–20 rule, 80% of your site’s value may come from only 20% of what’s in your Drupal installation. We can expend time and budget on getting everything into WordPress but it might not be worth the investment. The migration is therefore be a good time to clean-up your site of any un-needed bloat. Your main challenge during the migration project is to understand which parts of your Drupal site is valuable so you can instruct me on what to convert into WordPress.

Questions to define migration requirements

To help focus your efforts at the beginning of your Drupal to WordPress migration, here’s list of questions to ask yourself or your content management team:

  • Which content types do you want to migrate?
  • Which of the above content types should be converted to WordPress pages and which should be converted to WordPress posts? If you’re unfamiliar with the differences between posts and pages, see Post vs. Page on the WordPress support site.
  • Are any custom WordPress content types required?
  • Do you want to export some Drupal vocabularies or terms as WordPress categories? Generally, you should have relatively few categories and the remaining should be exported as WordPress tags.
  • Do you want to migrate comments?
  • Do you want to migrate authors?
  • What should be the WordPress default category?
  • What are your search engine optimisation (SEO) requirements?
  • Do you want to convert your existing design into a WordPress theme, do you want to design new theme or will you be happy with a ready-made theme?
  • How much of the Drupal site’s functionality can be replicated by existing WordPress plugins? If you’re willing to be flexible, it might be unnecessary to build any custom plugins

What’s needed to get started

Many of the above are simply things to think about during the early stages of the project. We won’t need answers to them immediately. To get started on a Drupal to WordPress migration, just send me the following:

  1. A MySQL dump file or access to your database.
  2. How you want to divide up the content types into pages and posts.
  3. How you want to divide up the terms into categories and tags.

Once we’re underway, I will guide you on what’s needed for each step of the process.

Drupal to WordPress migration activity diagram

This UML activity diagram accompanies my post, Drupal to WordPress migration explained.

Drupal to WordPress migration process activity diagram

Diagram created with draw.io.

Migration steps listing

  1. Prepare tables: This is where we reset the development database tables to a known state, ready for another migration pass.
  2. Delete unwanted vocabularies
  3. Delete unwanted terms
  4. Merge terms? Yes: go to step a; No: go to step 5
    1. Create tables for each vocabulary to merge
    2. Create duplicate table for each vocabulary
    3. Make duplicate terms unique
    4. Merge terms
  5. Create tags
  6. Create categories and sub-categories
  7. Set uncategorized term
  8. Create posts from nodes
  9. Set posts and page types
  10. Associate posts with terms
  11. Update tag counts
  12. Set default category
  13. Migrate comments
  14. Migrate authors
  15. Site-specific settings and customisation: this would include WordPress site information settings and URL redirects

Why is the Drupal term_node table missing?

In a Drupal to WordPress migration post by Sam Michel, reader Jean-Philippe commented that why he couldn’t find the tables mentioned. I started composing my reply but for some reason the page wouldn’t let me post.

Rather than waste the time it took to compose the reply, I thought it would be good to post it here instead.


Hi Jean-Philippe,

This probably isn’t relevant to you anymore but I’m posting this for the benefit of anyone else who stumbles across your comment.

The tables names have changed in Drupal 7. ‘taxonomy_term_data’ and ‘taxonomy_term_hierarchy’ are all Drupal 7 tables. If you don’t see the tables mentioned in this post, it’s almost certainly because you don’t have Drupal 6 installed.

In Drupal 7, ‘term_node’ has been replaced with ‘taxonomy_index’.

I’ve started documenting the table mapping between Drupal and WordPress in a series of posts here: https://anothercoffee.net/drupal-to-wordpress-migration-posts-table-mapping/

Since I also used Scott Anderson’s SQL, anyone reading Sam Michel’s series might find it useful too. (Thanks to Sam and Scott!)

FeenBan: A shadowban plugin for WordPress

Michael W. Dean of the Freedom Feens Talk Radio Show was having a problem with concern trolls. He wanted a way to shadowban commenters so I made him a plugin.

Here’s what he had to say about it:

“Gets rid of trolls without them knowing they’ve been banned. They keep posting, but no one other than them can see their posts.

Deliciously devilish! Trolling the trolls!

FeenBan! I’m not just the namesake, I’m also a client!”

For more information, visit the plugin page or download it from the WordPress Plugin Directory.

Drupal to WordPress migration: user table mapping

This is part four of a series of posts documenting the table mappings for a site migration from Drupal 6 to WordPress 3. For more information, please see the first article in the series.

Table mapping for WordPress users

This maps Drupal user export to WordPress.

Drupal 6.x

WordPress 3.x

Notes

users

wp_posts

uid

ID

name

user_login

Format to lowercase, replace spaces with underscores

pass

user_pass

name

user_nicename

mail

user_email

created

user_registered

Formatted from UNIX time

name

display_name

user_status

Whitespace string

user_activation_key

Set to 0

Table mapping for WordPress user meta values

User information like capabilities and roles in the wp_usermeta table.

users

wp_usermeta

uid

user_id

meta_key

Set to string e.g. ‘wp_capabilities’

meta_value

Set to string e.g. ‘a:1:{s:6:”author”;s:1:”1″;}’

More information about the settings for appropriate meta_key and meta_value can be found in the WordPress Codex:

Node authors and comment authors

Drupal stores both node authors and comment authors in the users table. WordPress handles things differently. Page and post authors are stored in the wp_users table but comment authors are stored in wp_comments together with the comment data.

Drupal to WordPress migration: comments table mapping

This is part three of a series of posts documenting the table mappings for a site migration from Drupal 6 to WordPress 3. For more information, please see the first article in the series.

Table mapping for WordPress comments

Drupal 6.x

WordPress 3.x

Notes

comments

wp_posts

cid

comment_ID

nid

comment_post_ID

timestamp

comment_date

Converted from UNIX timestamp

comment

comment_content

pid

comment_parent

name

comment_author

mail

comment_author_email

homepage

comment_author_url

Truncated to WordPress limit of 200 chars

status

comment_approved

Comment authors

A note about the different ways Drupal and WordPress store comment author information: Drupal stores comment authors in its users table alongside site users like node authors. In WordPress, comment authors are stored in its wp_comments together with the comment data. WordPress comment authors are not entered into the wp_users table.

Drupal to WordPress migration: terms table mapping

This is part two of a series of posts documenting the table mappings for a site migration from Drupal 6 to WordPress 3. For more information, please see the first article in the series.

Table mapping for WordPress terms

This table mapping exports the Drupal terms into WordPress.

Drupal 6.x

WordPress 3.x

Notes

term_data

wp_terms

tid

term_id

name

name

name

slug

Make lower case and convert spaces to underscores

vid

term_group

Not used in a default WordPress installation

term_data

wp_term_taxonomy

tid

term_taxonomy_id

tid

term_id

taxonomy

String: ‘post_tag’ or ‘category’

description

description

parent

0 (No parent)

In the WordPress Taxonomy documentation, “term_group is a means of grouping together similar terms.” During a standard migration, the WordPress term_group is set to the Drupal vocabulary ID, which seems to make sense. Nevertheless, a default WordPress installation does not actually use the value for anything. It may have been included by the developers for future expandability or use by plugins.

term_group=0 is the default value when creating a term using the Drupal user interface.

Below, we associate posts with the newly migrated terms.

Drupal 6.x

WordPress 3.x

term_node

wp_term_relationships

nid

object_id

tid

term_taxonomy_id

Drupal to WordPress migration: posts table mapping

Following on from Drupal to WordPress migration explained, I will create a series of posts documenting the table mappings for a site migration from Drupal 6.x to WordPress 3.x.

To read the mapping, you look up the Drupal table on the left listing the fields we use for a migration. Directly to its right is the WordPress table with the corresponding field in the same row. So for example, the nid in Drupal’s node table is exported to the id field in the WordPress wp_posts table.

I have listed all the fields used in the query. If a Drupal field shows no mapping in the WordPress table, it is being used to match entries in another table for a join. Here we use the vid field in node and node_revisions for an INNER JOIN.

Table mapping for WordPress wp_posts

Drupal 6.x

WordPress 3.x

Notes

node

wp_posts

nid

id

id

post_author

created

post_date

Create date from UNIX timestamp

title

post_title

changed

post_modified

Create date from UNIX timestamp

type

post_type

status

post_status

vid

node_revisions

body

post_content

teaser

post_excerpt

vid

node

url_alias

nid

dst

post_name

If dst field is NULL, use nid

src

to_ping

Whitespace string

pinged

Whitespace string

post_content_filtered

Whitespace string

Query

REPLACE INTO wordpress.wp_posts (
id,
post_author,
post_date,
post_content,
post_title,
post_excerpt,
post_name,
post_modified,
post_type,
post_status,
to_ping,
pinged,
post_content_filtered)
SELECT DISTINCT
n.nid ‘id’,
n.uid ‘post_author’,
DATE_ADD(FROM_UNIXTIME(0), interval n.created second) ‘post_date’,
r.body ‘post_content’,
n.title ‘post_title’,
r.teaser ‘post_excerpt’,
IF(a.dst IS NULL,n.nid, SUBSTRING_INDEX(a.dst, ‘/’, -1)) ‘post_name’,
DATE_ADD(FROM_UNIXTIME(0), interval n.changed second) ‘post_modified’,
n.type ‘post_type’,
IF(n.status = 1, ‘publish’, ‘private’) ‘post_status’,
‘ ‘,
‘ ‘,
‘ ‘
FROM drupal.node n
INNER JOIN drupal.node_revisions r USING(vid)
LEFT OUTER JOIN drupal.url_alias a
ON a.src = CONCAT(‘node/’, n.nid)
WHERE n.type IN (
/* List the content types you want to migrate */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Drupal to WordPress migration SQL queries explained

In this post I will give a step-by-step explanation of my Drupal to WordPress migration SQL queries. For general information about migrating from Drupal to WordPress, please see instead my Drupal to WordPress Migration Guide.

Drupal to WordPress migration queries screenshot

Since I offer site migration as a paid service, readers might be wondering why I’m giving away some of my secret sauce. The simple answer is that the ingredients of the sauce are anything but secret. A web search brings up blog posts and tutorials detailing how to go about it. In fact, the first version of my own Drupal to WordPress migration tool was based on a blog post by another web company.

However, while the knowledge is freely available, the whole process can be a real pain, especially when you’re dealing with a Drupal installation with lots of content and customisations. In my experience, migrating an established site from Drupal to WordPress requires all of the following:

  • An intermediate to advanced level of technical skill;
  • Time to plan, run the migration and do post-migration clean-up;
  • A great deal of patience.

If you are a developer with the right skills, you still need to consider the time investment and effort needed to understand both Drupal and WordPress database schemas. Often, the man-hours required will make a client think twice about proceeding with the project. Fortunately (or unfortunately) for me, I’ve needed to put in the time because my maintenance agreement for some clients covered exactly this sort of work. I’ve therefore built up enough experience to offer competitive quotes for migration of even complex sites. In my very biased opinion, you’ll make better use of your time and budget by getting me to do the task!

Of course, there are still cases where, for whatever reason, it’s not viable to offload the work to someone else. If this sounds like your situation, you’ll probably figure things out eventually so I might as well help you along.

Prerequisites for the Drupal to WordPress migration script

To run this migration you will need:

  • A working installation of Drupal 6;
  • A clean installation of WordPress 3.5 or above;
  • Access to the Drupal and WordPress MySQL databases;
  • The ability to run SQL queries on both databases;
  • Both databases on the same MySQL server;
  • Access to the Drupal and WordPress installations.

Ideally, you should:

  • Run the migration from a development server (it’s best not to risk running this on your live server);
  • Backup your live Drupal database before beginning the migration;
  • Be comfortable with running database operations;
  • Have planned the migration beforehand (e.g. which content types and taxonomies should be migrated).

If you have an older version of Drupal, simply upgrade to Drupal 6 first, then run the migration. I mention WordPress 3.5 as a prerequisite because that’s where I’ve done most testing. You can probably get away with migrating straight into a more recent version but to avoid any problems, I suggest you start off with WordPress 3.5. It’s easy to upgrade to the latest WordPress version after converting the Drupal content.

The Drupal to WordPress migration SQL queries

Keep in mind that this article is based on my Drupal to WordPress migration tool but with some additional queries written for the specific needs of a client. It therefore includes some values which will not apply to your site. I’ve stripped out any identifying information but left generic data to provide an example. You will need to manually look up the correct values in the Drupal database for your installation.

Another thing to keep in mind is that I’ve favoured readability over efficiency. For example, it’s possible to write more complex queries to avoid creating working tables but that would make debugging more difficult. Having a trail of data changes can help with content analysis if the end results aren’t what you expected.

CAUTION: Make a backup of both your Drupal and WordPress databases before running these queries. USE IS ENTIRELY AT YOUR OWN RISK. I’m offering this information with no warranty or support implied.

Example data in this scenario

  • wordpress: the WordPress database name.
  • drupal: the Drupal database name.
  • acc_ table prefixes: these are working tables I create to help with migrating data.

Clear out some Drupal and WordPress tables

For most migration projects to date, I’ve needed to run through several passes of the queries as I make incremental adjustments based on the client’s feedback. I define a ‘pass’ as one iteration of the entire migration process and inspecting the results in a WordPress installation. Depending on your project requirements, you may need to do this several times, making little tweaks to the MySQL queries as you go along.

These Drupal and WordPress tables may be empty or non-existent if you’re running the queries for the first time but it makes sense to clear them out at the start.

TRUNCATE TABLE wordpress.wp_comments;
TRUNCATE TABLE wordpress.wp_links;
TRUNCATE TABLE wordpress.wp_postmeta;
TRUNCATE TABLE wordpress.wp_posts;
TRUNCATE TABLE wordpress.wp_term_relationships;
TRUNCATE TABLE wordpress.wp_term_taxonomy;
TRUNCATE TABLE wordpress.wp_terms;
TRUNCATE TABLE wordpress.wp_users;

DROP TABLE IF EXISTS drupal.acc_duplicates;
DROP TABLE IF EXISTS drupal.acc_news_terms;
DROP TABLE IF EXISTS drupal.acc_tags_terms;
DROP TABLE IF EXISTS drupal.acc_wp_tags;
DROP TABLE IF EXISTS drupal.acc_users_post_count;
DROP TABLE IF EXISTS drupal.acc_users_comment_count;
DROP TABLE IF EXISTS drupal.acc_users_with_content;
DROP TABLE IF EXISTS drupal.acc_users_post_count;

For some installations, I make changes to the wp_usermeta table so that needs to be cleared too.

TRUNCATE TABLE wordpress.wp_usermeta;

Vocabularies and taxonomies

Delete unwanted vocabularies. You’ll need to look in your Drupal vocabulary table for the appropriate vids. In this case, I’m deleting vocabularies 5, 7, 8, 38 and 40.

DELETE FROM drupal.vocabulary WHERE vid IN (5, 7, 8, 38, 40);

Delete terms associated with unwanted vocabularies. Here I’m keeping the terms for vid 38. Sometimes you might want to keep some terms of unwanted vocabularies for later conversion into into WordPress tags. (Please see the next query.)

DELETE FROM drupal.term_data WHERE vid IN (5, 7, 8, 40);

You may want to merge terms. In this example, I am merging the previously saved terms for News which has vid 38 to the Tags vocabulary terms which has vid 2.

We will need to deal with duplicates. For example, in the Drupal installation, ‘science’ could appear in both News (vid 38) and Tags (vid 2). This will cause a problem when exporting to WordPress since we can’t have duplicate terms. Here I create working tables for both term groups.

CREATE TABLE drupal.acc_news_terms AS SELECT tid, vid, name FROM drupal.term_data WHERE vid=38;
CREATE TABLE drupal.acc_tags_terms AS SELECT tid, vid, name FROM drupal.term_data WHERE vid=2;

Create a working table from duplicates.

CREATE TABLE drupal.acc_duplicates AS
SELECT t.tid tag_tid,
n.tid news_tid,
t.vid tag_vid,
n.vid news_vid,
t.name
FROM drupal.acc_tags_terms AS t
INNER JOIN (drupal.acc_news_terms AS n)
ON n.name=t.name;

Append a string to News terms duplicates so they won’t clash during migration. Here I used a fixed string but this won’t work if you have more than two terms with the same name. If you expect many terms with the same name, it would be better to generate a unique number. For example, using the tid would make it unique since these are unique primary keys. Use whatever string makes sense for your project.

Note that we’re overwriting the source data in the Drupal term_data table so proceed with care. Make sure you have a backup of your pre-migration Drupal tables in case you need to run the conversion again.

UPDATE drupal.term_data
SET name=CONCAT(name, ‘_01’)
WHERE tid IN (SELECT news_tid FROM drupal.acc_duplicates);

Convert Drupal News terms to Drupal Tags. We’ll migrate the whole lot into WordPress tags later.

UPDATE drupal.term_data SET vid=2 WHERE vid=38;

Create a table of WordPress tags. Exclude any terms from Drupal vocabularies that you might later migrate into WordPress categories. See the MySQL queries below where I create WordPress categories and sub-categories.

Here, all Drupal vocabularies except 37, 36 and 35 will be converted WordPress tags.

CREATE TABLE drupal.acc_wp_tags AS
SELECT
tid,
vid,
name
FROM drupal.term_data
WHERE vid NOT IN (37, 36, 35);

Now create the tags in the WordPress database. A clean WordPress database will have term_id=1 for ‘Uncategorized’. Use REPLACE as this may conflict with a Drupal tid.

We are assuming that this point the Drupal term_data table has been cleaned of any duplicate names. Any duplicate terms will be lost when running this MySQL query.

REPLACE INTO wordpress.wp_terms (term_id, name, slug, term_group)
SELECT
d.tid,
d.name,
REPLACE(LOWER(d.name), ‘ ‘, ‘_’),
d.vid
FROM drupal.term_data d WHERE d.tid IN (
SELECT t.tid FROM drupal.acc_wp_tags t
);

In WordPress, tags and categories are all stored in the wp_term_taxonomy table. The taxonomy field specifies whether it’s a tag or category by setting the field string to either ‘post_tag’ or ‘category’.

Here I convert these Drupal terms into WordPress tags.

REPLACE INTO wordpress.wp_term_taxonomy (
term_taxonomy_id,
term_id,
taxonomy,
description,
parent)
SELECT DISTINCT
d.tid,
d.tid ‘term_id’,
‘post_tag’, /* This string makes them WordPress tags */
d.description ‘description’,
0 /* In this case, I don’t give tags a parent */
FROM drupal.term_data d
WHERE d.tid IN (SELECT t.tid FROM drupal.acc_wp_tags t);

Create the categories and sub-categories in the WordPress database. This may be unnecessary depending on your setup.

Add terms associated with a Drupal vocabulary into WordPress. Note that in this case, these are the same vids that I excluded from the tag table above.

REPLACE INTO wordpress.wp_terms (term_id, name, slug, term_group)
SELECT DISTINCT
d.tid,
d.name,
REPLACE(LOWER(d.name), ‘ ‘, ‘_’),
d.vid
FROM drupal.term_data d
WHERE d.vid IN (37, 36, 35);

Convert these Drupal terms into WordPress sub-categories by setting the parent field in the wp_term_taxonomy table.

REPLACE INTO wordpress.wp_term_taxonomy (
term_taxonomy_id,
term_id,
taxonomy,
description,
parent)
SELECT DISTINCT
d.tid,
d.tid ‘term_id’,
‘category’,
d.description ‘description’,
d.vid
FROM drupal.term_data d
WHERE d.vid IN (37, 36, 35);

Now add the vocabularies to the WordPress terms table. There’s no need to set term_id as vocabularies are not directly associated with posts.

INSERT INTO wordpress.wp_terms (name, slug, term_group)
SELECT DISTINCT
v.name,
REPLACE(LOWER(v.name), ‘ ‘, ‘_’),
v.vid
FROM drupal.vocabulary v
WHERE vid IN (37, 36, 35);

Insert Drupal vocabularies as WordPress categories.

INSERT INTO wordpress.wp_term_taxonomy (
term_id,
taxonomy,
description,
parent,
count)
SELECT DISTINCT
v.vid,
‘category’, /* This string makes them WordPress categories */
v.description,
v.vid,
0
FROM drupal.vocabulary v
WHERE vid IN (37, 36, 35);

Update the WordPress term groups and parents.

Before continuing with this step, we need to manually inspect the table to get the term_id for the parents inserted above. In this case, vids 37, 36 and 35 were inserted as into the wp_term_taxonomy table as term_ids 7517, 7518 and 7519. I will use them as the parents for their respective terms. In other words, terms that formerly belonged to the Drupal vocabulary ID 37 would now belong to the WordPress parent category 7519.

UPDATE wordpress.wp_terms SET term_group=7519 WHERE term_group=37;
UPDATE wordpress.wp_terms SET term_group=7518 WHERE term_group=36;
UPDATE wordpress.wp_terms SET term_group=7517 WHERE term_group=35;

UPDATE wordpress.wp_term_taxonomy SET parent=7519 WHERE parent=37;
UPDATE wordpress.wp_term_taxonomy SET parent=7518 WHERE parent=36;
UPDATE wordpress.wp_term_taxonomy SET parent=7517 WHERE parent=35;

UPDATE wordpress.wp_term_taxonomy SET term_id=7519 WHERE term_taxonomy_id=7519;
UPDATE wordpress.wp_term_taxonomy SET term_id=7518 WHERE term_taxonomy_id=7518;
UPDATE wordpress.wp_term_taxonomy SET term_id=7517 WHERE term_taxonomy_id=7517;

Re-insert the Uncategorized term replaced earlier in the conversion process. We may have replaced or deleted the Uncategorized category during a previous MySQL query. Re-insert it if you want an Uncategorized category.

INSERT INTO wordpress.wp_terms (name, slug, term_group)
VALUES (‘Uncategorized’, ‘uncategorized’, 0);
INSERT INTO wordpress.wp_term_taxonomy (
term_taxonomy_id,
term_id,
taxonomy,
description,
parent,
count)
SELECT DISTINCT
t.term_id,
t.term_id,
‘category’,
t.name,
0,
0
FROM wordpress.wp_terms t
WHERE t.slug=’uncategorized’;

Converting Drupal nodes to WordPress posts

Now create WordPress posts from Drupal nodes. This may take a while if you have many Drupal nodes. Wait until the query completes before continuing. It could take several minutes.

REPLACE INTO wordpress.wp_posts (
id,
post_author,
post_date,
post_content,
post_title,
post_excerpt,
post_name,
post_modified,
post_type,
post_status,
to_ping,
pinged,
post_content_filtered)
SELECT DISTINCT
n.nid ‘id’,
n.uid ‘post_author’,
DATE_ADD(FROM_UNIXTIME(0), interval n.created second) ‘post_date’,
r.body ‘post_content’,
n.title ‘post_title’,
r.teaser ‘post_excerpt’,
IF(a.dst IS NULL,n.nid, SUBSTRING_INDEX(a.dst, ‘/’, -1)) ‘post_name’,
DATE_ADD(FROM_UNIXTIME(0), interval n.changed second) ‘post_modified’,
n.type ‘post_type’,
IF(n.status = 1, ‘publish’, ‘private’) ‘post_status’,
‘ ‘,
‘ ‘,
‘ ‘
FROM drupal.node n
INNER JOIN drupal.node_revisions r USING(vid)
LEFT OUTER JOIN drupal.url_alias a
ON a.src = CONCAT(‘node/’, n.nid)
WHERE n.type IN (
/* List the content types you want to migrate */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Set the Drupal content types that should be migrated as WordPress ‘posts’. In this case, I want ‘page’, ‘story’, ‘blog’, ‘video’, ‘forum’ and ‘comment’ in Drupal to be converted to posts in WordPress.

UPDATE wordpress.wp_posts SET post_type = ‘post’
WHERE post_type IN (
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Now convert the remaining content types into WordPress pages.

UPDATE wordpress.wp_posts SET post_type = ‘page’ WHERE post_type NOT IN (‘post’);

Housekeeping queries for terms

Here I associate the content with WordPress terms using the wp_term_relationships table.

INSERT INTO wordpress.wp_term_relationships (
object_id,
term_taxonomy_id)
SELECT DISTINCT nid, tid FROM drupal.term_node;

We need to update tag counts.

UPDATE wordpress.wp_term_taxonomy tt
SET count = ( SELECT COUNT(tr.object_id)
FROM wordpress.wp_term_relationships tr
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id);

Now set the default WordPress category. You’ll need to manually look in the database for the term_id of the category you want to set as the default WordPress category.

UPDATE wordpress.wp_options SET option_value=’7520′ WHERE option_name=’default_category’;
UPDATE wordpress.wp_term_taxonomy SET taxonomy=’category’ WHERE term_id=7520;

Migrate comments

REPLACE INTO wordpress.wp_comments (
comment_ID,
comment_post_ID,
comment_date,
comment_content,
comment_parent,
comment_author,
comment_author_email,
comment_author_url,
comment_approved)
SELECT DISTINCT
cid,
nid,
FROM_UNIXTIME(timestamp),
comment,
pid,
name,
mail,
SUBSTRING(homepage,1,200),
((status + 1) % 2) FROM drupal.comments;

Update comment counts.

UPDATE wordpress.wp_posts
SET comment_count = ( SELECT COUNT(comment_post_id)
FROM wordpress.wp_comments
WHERE wordpress.wp_posts.id = wordpress.wp_comments.comment_post_id);

Migrate Drupal Authors into WordPress

In this case I am migrating only users who have created a post. This was a requirement for my project but may be unnecessary for you.

First delete all existing WordPress authors except for admin.

DELETE FROM wordpress.wp_users WHERE ID > 1;
DELETE FROM wordpress.wp_usermeta WHERE user_id > 1;

Now set Drupal’s admin password to a known value. This avoids hassles with trying to reset the password on the new WordPress installation. Resetting a user password in WordPress is more convoluted and cannot be done using a simple MySQL query as in Drupal.

UPDATE drupal.users set pass=md5(‘password’) where uid = 1;

Create a working table of users and the number of posts they’ve authored. I am only considering authors who have created posts of the content types I want to migrate.

CREATE TABLE drupal.acc_users_post_count AS
SELECT
u.uid,
u.name,
u.mail,
count(n.uid) node_count
FROM drupal.node n
INNER JOIN drupal.users u on n.uid = u.uid
WHERE n.type IN (
/* List the post types I migrated earlier */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’)
GROUP BY u.uid
ORDER BY node_count;

Now add these authors into the WordPress wp_users table.

INSERT IGNORE INTO wordpress.wp_users (
ID,
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
user_activation_key,
user_status,
display_name)
SELECT DISTINCT
u.uid,
REPLACE(LOWER(u.name), ‘ ‘, ‘_’),
u.pass,
u.name,
u.mail,
FROM_UNIXTIME(created),
”,
0,
u.name
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_post_count);

First set all these authors to WordPress “author” by default. In the next MySQL query, we can selectively promote individual authors to other WordPress roles.

INSERT IGNORE INTO wordpress.wp_usermeta (
user_id,
meta_key,
meta_value)
SELECT DISTINCT
u.uid,
‘wp_capabilities’,
‘a:1:{s:6:”author”;s:1:”1″;}’
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_post_count);

INSERT IGNORE INTO wordpress.wp_usermeta (
user_id,
meta_key,
meta_value)
SELECT DISTINCT
u.uid,
‘wp_user_level’,
‘2’
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_post_count);

During the course of the migration, some posts may end up not having an assigned author. Here I reassign authorship for these posts to the WordPress admin user.

UPDATE wordpress.wp_posts
SET post_author = 1
WHERE post_author NOT IN (SELECT DISTINCT ID FROM wordpress.wp_users);

Comment authors

In Drupal, comments are treated as nodes and comment authors are stored along with other node authors in the Drupal users table. WordPress treats comments and comment authors differently. Comment authors in WordPress are not stored in the wp_users table. Instead, they’re stored along with the comment content itself in the wp_comments table.

We may need to run additional query to import users who have commented but haven’t created any of the selected content types. To do this, I create some working tables required for some later MySQL queries:

  • acc_users_with_comments: empty copy of wp_users
  • acc_users_add_commenters: empty copy of wp_users
  • acc_wp_users: copy of wp_users from wordpress database containing users

Running the following MySQL queries will throw errors if you haven’t created the required tables above.

First create a working table of Drupal users who have created a Drupal comment.

CREATE TABLE drupal.acc_users_comment_count AS
SELECT
u.uid,
u.name,
count(c.uid) comment_count
FROM drupal.comments c
INNER JOIN drupal.users u on c.uid = u.uid
GROUP BY u.uid;

Now add the author information for these users into another working table.

INSERT IGNORE INTO drupal.acc_users_with_comments (
ID,
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
user_activation_key,
user_status,
display_name)
SELECT DISTINCT
u.uid,
REPLACE(LOWER(u.name), ‘ ‘, ‘_’),
u.pass,
u.name,
u.mail,
FROM_UNIXTIME(created),
”,
0,
u.name
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_comment_count);

Using the above table, next build a working table of Drupal users who have commented but have not already been added to the WordPress wp_users table.

INSERT IGNORE INTO drupal.acc_users_add_commenters (
ID,
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
user_activation_key,
user_status,
display_name)
SELECT DISTINCT
u.ID,
u.user_login,
u.user_pass,
u.user_nicename,
u.user_email,
u.user_registered,
”,
0,
u.display_name
FROM drupal.acc_users_with_comments u
WHERE u.ID NOT IN (SELECT ID FROM drupal.wp_users);

Combine the tables into another working table acc_wp_users.

INSERT IGNORE
INTO drupal.acc_wp_users
SELECT *
FROM drupal.acc_users_add_commenters;

The acc_wp_users working table helps when inspecting the user list. For example, you might want to clear out inactive users or spam posters. Once finished, remember to replace your WordPress wp_users with the cleaned acc_wp_users table. You may prefer to amend the above query to insert directly into the WordPress wp_users table.

I realise this is a rather round-about way of migrating comment authors from Drupal into WordPress but find that having working tables helps with debugging.

Housekeeping for WordPress options

Update file path for the WordPress installation.

UPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, ‘”/files/’, ‘”/wp-content/uploads/’);

Set your WordPress site name using the Drupal ‘site_name’ variable.

UPDATE wordpress.wp_options SET option_value = ( SELECT value FROM drupal.variable WHERE name=’site_name’) WHERE option_name = ‘blogname’;

Set your WordPress site description using the Drupal ‘site_slogan’ variable.

UPDATE wordpress.wp_options SET option_value = ( SELECT value FROM drupal.variable WHERE name=’site_slogan’) WHERE option_name = ‘blogdescription’;

Set the WordPress site email address.

UPDATE wordpress.wp_options SET option_value = ( SELECT value FROM drupal.variable WHERE name=’site_mail’) WHERE option_name = ‘admin_email’;

Set the WordPress permalink structure. Here we’re using /%postname%/ but you may set it according your own needs.

UPDATE wordpress.wp_options SET option_value = ‘/%postname%/’ WHERE option_name = ‘permalink_structure’;

Create URL redirects table

This table will not be used for the migration but may be useful if you need to manually create redirects from Drupal aliases. You will need the entries here for search engine optimisation (SEO) of your new WordPress site.

DROP TABLE IF EXISTS drupal.acc_redirects;
CREATE TABLE drupal.acc_redirects AS
SELECT
CONCAT(‘drupal/’,
IF(a.dst IS NULL,
CONCAT(‘node/’, n.nid),
a.dst
)
) ‘old_url’,
IF(a.dst IS NULL,n.nid, SUBSTRING_INDEX(a.dst, ‘/’, -1)) ‘new_url’,
‘301’ redirect_code
FROM drupal.node n
INNER JOIN drupal.node_revisions r USING(vid)
LEFT OUTER JOIN drupal.url_alias a
ON a.src = CONCAT(‘node/’, n.nid)
WHERE n.type IN (
/* List the post types I migrated earlier */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Finalising the conversion

Now that we’ve finished converting the content over from Drupal to WordPress, we have the rather (not very) fun job of checking the content, setting up any WordPress plugins and widgets, then finally going live. Depending on the complexity of your Drupal installation, this process can be extremely time-consuming and perhaps form a separate project in its own right.

You can use my Drupal to WordPress migration notes to help with going live. Of course, the search for equivalent WordPress plugins and conversion from the old Drupal modules will have to be done according to your specific set-up.

Accepting limitations

Before finishing this article, one point I’d like to convey is the importance of accepting the limitations of any migration process. You, or your client, may be insistent on turning your new WordPress site into an exact copy of your former Drupal installation. While this may be technically possible, there comes a point of diminishing returns where the work you’d need to put in just isn’t worth the value of the data.

It may also be more productive in some instances to make manual adjustments via the WordPress control panel than to try anything clever using the backend database. Sometimes the time needed to write, test and debug MySQL queries far exceeds the boring but more reliable editing using the web-front end.

Expect to kill your SEO if you’re not careful with the migration. A site that relies heavily on revenue from search engine rankings will need extra steps to preserve SEO. This is a huge topic so I will not cover it here but you should carefully plan the conversion steps before starting with the migration. Pay particular attention to preserving Drupal path aliases, taxonomy listing pages and internal links.

Good luck!

So that’s it. A Drupal to WordPress migration can be a great deal of effort. I’ve had one project that initially looked like only couple of hours work balloon to over 50 billable hours in total. Previously installed modules caused problems requiring custom MySQL queries and PHP scripting to resolve. On the other hand, I’ve had a number of sites that completed in 15 minutes after running my Drupal to WordPress migration tool.

Overall, the majority of my clients prefer WordPress over their previous Drupal site. I personally find WordPress quicker to update and manage. Any short-term hassles with migrating have been outweighed by the long-term advantages of easier maintenance.

Getting the code and submitting improvements

You can find the code on my GitHub repository. The queries in this article can be found in the file drupaltowordpress-custom.sql.

I’d love to receive corrections, bug fixes and suggestions for improvements. Please contact me or submit an issue on GitHub.

CAUTION: Make a backup of both your Drupal and WordPress databases before running these queries. USE IS ENTIRELY AT YOUR OWN RISK. I’m offering this information with no warranty or support implied.