After using our plugin to migrate Drupal to WordPress, you may want to convert a custom field to the body content of a post. While this is a general tutorial it can be applied to many cases.

With this tutorial you will learn how to convert a custom field to the body content of a post eliminating the need of extra PHP coding to display those fields.

In our example, you just finished the migration from Drupal to WordPress, using our plugin https://www.fredericgilles.net/fg-drupal-to-wordpress/

In the backend everything seems fine. The issue is that when you view your posts, you cannot see the migrated data. This happens because our plugin migrated the Drupal data to custom post types.

Unfortunately there is no magic way for our plugin to know beforehand your preferences. This means that it cannot know if the Drupal field you want to migrate should be in the WordPress body or excerpt field.

Fortunately there is a solution for both of those cases.

1) You want to convert a custom field to the WordPress body

What you will have to do is to run two simple MySQL queries at your phpMyAdmin. To do this correctly please follow the steps bellow:

Step 1: Always backup

Please take a backup of your WordPress database.

To do this open your phpMyAdmin and click the Export tab:

backup your wordpress database

Then click the Go button and wait for your database to download.

exporting wordpress database from phpmyadmin

Once the download is finished you can go to the next step.

Step 2: Run the code

The code to migrate the Drupal fields to the WordPress body is:

UPDATE wp_posts p
SET post_content =
(SELECT meta_value
FROM wp_postmeta
WHERE meta_key = "wpcf-myfield"
AND post_id = p.ID
LIMIT 1)
WHERE post_type = "mycpt";

To use the code, open your phpMyAdmin and click the “SQL” tab:

convert a custom field to wordpress fields with an sql query in phpmyadmin

Then copy and paste the above code inside the input box:

the sql query to convert a custom field to wordpress fields with an sql query in phpmyadmin

and finally click Go.

Run the sql query to convert a custom field
Attention:

Please do not forget to change the “myfield” text at the line:

WHERE meta_key = "wpcf-myfield"

to the name of the Drupal field you want to migrate.

And change the “mycpt” text at the line:

WHERE post_type = "mycpt";

Step 3: Clean up

Then you will have to run a second SQL query to clean from WordPress the Drupal custom fields.

To do this run the following SQL code at your phpMyAdmin:

DELETE FROM wp_postmeta
WHERE meta_key = "wpcf-myfield";

And finally click Go.

Run the sql query to delete Drupal residues from converting custom

2) You want to move a custom field to the WordPress excerpt field

You can repeat the same steps to migrate the Drupal field to the WordPress excerpt field.

For the first SQL query:

The change on the code is on the second line:

UPDATE wp_posts p
SET post_excerpt =
(SELECT meta_value
FROM wp_postmeta
WHERE meta_key = "wpcf-myfield"
AND post_id = p.ID
LIMIT 1)
WHERE post_type = "mycpt";

The second SQL query it will be the same:

DELETE FROM wp_postmeta
WHERE meta_key = "wpcf-myfield";

Attention:

Once more, please do not forget to change the “myfield” text at the line:

WHERE meta_key = "wpcf-myfield"

to the name of the Drupal field you want to migrate.

And change the “mycpt” text at the line:

WHERE post_type = "mycpt";

Congratulations, you have converted a custom field to the WordPress field of your choice!

Are you looking for a tutorial on how to convert a custom post type to a standard WordPress post? Check our tutorial here!

LinkedIn
Share
RSS