Title: importing external data into custom fields
Last modified: August 18, 2016

---

# importing external data into custom fields

 *  [David Bisset](https://wordpress.org/support/users/dimensionmedia/)
 * (@dimensionmedia)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/importing-external-data-into-custom-fields/)
 * i’m trying to get posts from a custom DB into WP via wp-import, and I managed
   to get it working. however, each post from the custom DB has a single image associated
   with it that’s stored in the DB as well. Is there a way to export this image 
   data, along with post title, body, etc. into WP as a custom field?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/importing-external-data-into-custom-fields/#post-245151)
 * Short answer: probably.
 * Somewhat longer answer, it all depends on if your image data is associated with
   the post content during import (it could be imported later, but you’ll probably
   have to provide this association (the post ID) manually).
 * First, I assume based on a previous forum post, you used a customized method 
   to import through the MT importer. I can imagine adding to your import file a
   line in the header of each post (where TITLE, AUTHOR, etc are specified), something
   like the following for your image data :
 * `CUSTOM: /path/to/image.jpg`
 * In import-mt.php, you would have to edit in a new switch that handles the import’s
   $metadata:
 * `case 'CUSTOM':
    $custom_data = $value; break;
 * Then you’d have to incorporate the database query, probably just before the script
   prints out “Post imported successfully…”:
 * `if (!$wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE post_id = $post_id 
   AND meta_value = $custom_data")) {
    $wpdb->query("INSERT INTO $wpdb->postmeta(
   post_id, meta_key, meta_value) VALUES ($post_id, 'image', $custom_data) "); }
 * Note I inserted ‘image’ as the meta_key; use whatever makes sense here.
 *  Thread Starter [David Bisset](https://wordpress.org/support/users/dimensionmedia/)
 * (@dimensionmedia)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/importing-external-data-into-custom-fields/#post-245242)
 * Thanks! This seemed to work perfectly… just had to remember to put quotes around
   $custom_data because what I was trying to import was a string (“image123.jpg”).
 *  [spyderfcs](https://wordpress.org/support/users/spyderfcs/)
 * (@spyderfcs)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/importing-external-data-into-custom-fields/#post-246067)
 * I’m trying to do this and having issues. I added two fields: One for a path to
   an image and the other for tags.
 * I put these in the mt.php file in the import folder:
    ` case 'CUSTOM': $custom_data
   = $value; break; case 'TAGS': $tags = $value; break;
 * if (!$wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE post_id = $post_id AND
   meta_value = $custom_data")) {
    $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,
   meta_key, meta_value) VALUES ($post_id, 'image', $custom_data) "); } if (!$wpdb-
   >get_row("SELECT * FROM $wpdb->postmeta WHERE post_id = $post_id AND meta_value
   = $tags")) { $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value)
   VALUES ($post_id, 'photo', $tags) "); }
 * The importing goes fine but neither of the things I’ve added show up. Also…it’s
   not importing the comments…

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘importing external data into custom fields’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [spyderfcs](https://wordpress.org/support/users/spyderfcs/)
 * Last activity: [20 years, 3 months ago](https://wordpress.org/support/topic/importing-external-data-into-custom-fields/#post-246067)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
