WordPress.org

Forums

Importing Custom Fields from Movable Type (21 posts)

  1. Joe Siegler
    Member
    Posted 3 years ago #

    I'm looking to move two blogs to WordPress from some old MT installations I have. Thing is, both of them make quite heavy use of custom fields. Movable Type by default does not export that data, which is why I haven't moved to this date. Believe I've figured out a way to get the custom data exported, but my question is this.

    What does the import on WordPress' end do with custom data fields? Given they can be named anything, does WordPress just dump them into a custom field on it's end?

    Optimally what I'd like to be able to do is to take the exported Movable Type data, and import it into WordPress, routing their custom data fields into ones that I define on my own. Can that sort of rerouting, or mapping of the fields be done on import?

    Tkx.

  2. Joe Siegler
    Member
    Posted 3 years ago #

    Found this post talking about the subject:

    http://birdhouse.org/blog/2008/02/07/notes-on-a-massive-wordpress-migration/

    But it's three plus years old now. He mentions submitting it for a future version. Anyone know if it got included?

  3. esmi
    Forum Moderator
    Posted 3 years ago #

    It didn't get included in core, no. Try reviewing Importing_Content.

  4. jayseae
    Member
    Posted 3 years ago #

    @Joe, you can transfer custom fields, but it is not part of the core import (at least, it wasn't last time I checked).

    First you need to export the data, because by default MT does not do this - and getting it into the export is required for WP to import it.

    Second you need to update the import to process the data to the correct place. Since it isn't defined, you can pretty much tell it where to go - uh, hope you know what I mean. :)

    Essentially you need to map each field to the new location. It isn't horrible, but it can take some doing, especially if you have a lot of fields.

  5. Joe Siegler
    Member
    Posted 3 years ago #

    I figured out the bit about getting the Movable Type custom data exported, so that end is covered.

    I was just looking at a way to figure out how to route the custom fields to where I want them to go on the WP side.

    I did review Importing Content, that's where I got the link at birdhouse.org, to be honest. There doesn't appear to be info on exactly HOW to do this that I can find, which is why I posted.

    Tkx.

  6. Joe Siegler
    Member
    Posted 3 years ago #

    The easier of my two projects to move is a fan site I have for the Texas Rangers. For each game, I have results. In MT, the data about each game (outside of the blog entry) is in the following fields:

    1) Keywords: I have a URL there going to the official mlb.com recap.

    2) Tags: a four digit number which is the date of the game in question. (0915, for example)

    3) The one actual custom field called "Game Score", which is the score of the game.

    1 & 2 are part of the standard MT data structure, but I'm not using them in the way they were designed. In a perfect world, I'd want to take all three of these, and route them to a custom data field in WordPress, so I can deal with it that way.

    With no modification, I'm going to guess the WP importer will pick up 1 & 2, but dump 'em in those fields. Is there a way to reroute THOSE?

  7. Joe Siegler
    Member
    Posted 3 years ago #

    I created a short test version of my mt-export.txt from MT (which is 7.4Mb) containing just half a dozen entries.

    I rejiggered the movable type export script to pick up the custom data, so that end is done. I created a virgin WP blog and imported the short six entry text file to see what happens.

    1) The data that was in "keywords" in Movable Type was dumped into "tags" in WordPress

    2) What I had in "tags" in MT appears to be gone, I can't find where WP put it.

    3) Not imported, but I'm not surprised, it was a custom field.

    I can't find info on how to modify the wordpress importer to route these things where I want them to go (which would preferably be a custom field for each of them). There any links somewhere showing that?

    Edit: Just had an idea. Since the Movable Type export file is raw text, I could I suppose take the exported data, and change all three I want to be custom data fields, if I can figure out how to get custom data fields imported on the WP side.

  8. jayseae
    Member
    Posted 3 years ago #

    That's pretty much it.

    In the importer, you have tests like this:

    } else if ( 'BODY:' == $line ) {

    What you would do is add your own check:

    } else if ( 'FIELD:' == $line ) {

    Obviously, you would name the field appropriately here, so that it is uniquely identified - unless you want to save the body into a custom field.

    Then inside that test you would add the code to save it to the correct location, using the add_post_meta function.

    In order to do this, you'll need to have the post ID (so make sure you do it after the post is saved), the field name (which you could probably hardcode) and the field value (which you are reading from the exported MT file).

    That should do it for you.

  9. Joe Siegler
    Member
    Posted 3 years ago #

    Forgive the somewhat green posting, but how can I get the post ID on a post that's being imported? iIf I'm reading that right jayseae, I need to double import?

    There's several thousand entries in all, that would be a ton of work.

    Again, sorry if this all seems dumb. I'm sort of grasping this, but the lightbulb hasn't totally gone off yet. :)

    My first attempt just generated a white screen on the importer, so I obviously did something wrong. Will have to continue to play with it to find the right combination. At least I know I can get this imported. Tkx.

  10. jayseae
    Member
    Posted 3 years ago #

    Near the top of the process_posts subroutine in the importer, there is a line that looks like this:

    $result = $this->save_post($post, $comments, $pings);

    That happens at the end of each post's data, and it calls a routine (creatively called... "save_post"), and it is what actually inserts the post into the WP database. At that point, your post will have an ID.

    After that routine, you can use the ID, but the best thing to do is to first save each of your fields into a variable as you are processing them - perhaps an array, depending on the number (1-2 fields might be simpler as individual ones, otherwise an array with all the fields might be easier, that is up to you). If you do not save your fields, you will not have the ID in order to save them as custom fields, and you would have to save them elsewhere.

    Then once the post has been saved - and assuming that the post does not return because of an error - you have an ID, which means you can add code to handle the saving of your fields. This could be immediately after the code above, or it could be within the save_post routine itself, to keep things somewhat organized if you prefer that avenue.

    It would be nice if there was a way to add a filter to handle this, rather than hacking at the code itself, but it does not appear that you can do so - you need to process the text in the file and then you need to adjust/interrupt/piggyback on the save_post routine as well.

  11. Joe Siegler
    Member
    Posted 3 years ago #

    I've got to break for a bit, but this looks like good info to get me going in the right path. Thank you, and I'll report back later today on my progress. :)

  12. Joe Siegler
    Member
    Posted 3 years ago #

    OK, after a couple of hours of messing with this, I'm having difficulty figuring out the bit that will make it work. At first I was crashing the importer, and I'm not doing that, but it's not importing the data I'm talking about. Here's what I'm dealing with:

    http://www.rangerfans.com/mt-export.txt

    That's the short export file from Movable Type with about 6 entries or so in it. Not going to do the one with multiple thousands until I get the system locked down. :)

    There's three parts from each entry I want to import. They're these:

    TAGS:
    KEYWORDS:
    SCORE:

    "TAGS:" I want to import to a custom field called GAMEDATE
    "KEYWORDS:" I want to import to a custom field called MLBURL
    "SCORE:" I want to import to a custom field called SCORE

    Tags has a different format than the other two, plus it's not always there. In looking at the MT importer, it appears I'll have to come up with two bits of code to get this all imported. I've been trying with just tags so far before I moved to the others. The most recent attempt was this sequence of code.

    } else if ( 'TAGS:' == $line ) {
    				$tags = trim( substr($line, strlen('TAGS: ')) );
    			        add_post_meta($post_id, 'GAMEDATE', $tags);

    I found that line you're talking about, but if I put my code attempt RIGHT after that line,it munges up the import pretty good. If I put it down with all the other else if's, it doesn't crash anything, but nothing gets imported.

    Which tells me my code is screwed up. I tried replicating bits of code from the other import sections that work from similar lines around the "TAGS:" line in my import file, but they don't seem to work.

    This is obviously a non coder here trying to cobble this together, so if you have any further advice, I'd appreciate it.

  13. jayseae
    Member
    Posted 3 years ago #

    That's a bit more complex than where we started. :)

    What will happen - by default, mind you - is that tags will be imported as tags. I am fairly certain, but have not looked at it in that much detail, is that keywords will also be imported as tags. But put those two aside for the moment.

    The score is the only one addressed by the portion above. You would need to add a separate line to address that section. Something like this:

    } else if ( 'SCORE:' == $line ) {

    That would allow you to save the score into another field - call it $score for the sake of discussion - and then you could save that field into a meta field after saving the post. Something like:

    add_post_meta($post->ID, 'SCORE', $score);

    Where it gets (more) complicated is that you also want to redefine at least the tags function, to not save the tags as tags, but as a custom field, and perhaps do the same as keywords, which typically are saved as tags.

    That means those functions would need to be redefined - generally the same principle, but you would need to make sure the original functions were commented out or otherwise not being run (you could also remove them).

    Possible nevermind: In a quick search of the code, there is actually no reference to tags other than in reference to the comments - and I did not see any in your sample export either. So you may need to add this to your export and you would not have to worry about it on the import. Keywords may be the only issue here, the backwards of what I mentioned earlier.

  14. Joe Siegler
    Member
    Posted 3 years ago #

    There is the line TAGS: in a couple of my fields. It's not in all of them. If you looked at just the first on the linked code, you won't see it, but it's in all but two of the example entries.

    Keywords get imported as "tags" in WP if I don't mess with the code at all. My code attempt stopped them from being imported as tags, but didn't import it as a custom field, so something's happening there.

    I'll use your code fragment and see where I get.

  15. Joe Siegler
    Member
    Posted 3 years ago #

    Fair warning to my hair. Might get pulled out. :)

  16. Joe Siegler
    Member
    Posted 3 years ago #

    Delete post. nevermind.

  17. Joe Siegler
    Member
    Posted 3 years ago #

    Partial success. I managed to get one of them imported properly. The one I had on Movable Type as "KEYWORDS:". That's by default imported to the WP tags area. Thought that if it knew how to import that value, all I had to do was change where it goes in the importer. Looked around, and found this bit:

    wp_add_post_tags($post_id, $post->post_keywords);

    and replaced it with this bit:

    add_post_meta($post_id, 'MLBURL', $post->post_keywords);

    and it worked! That's one field down. Now to get the other two. :)

  18. Joe Siegler
    Member
    Posted 3 years ago #

    Second one down. Got "SCORE:" dealt with.

    After the section // Add tags or keywords, I added this bit:

    // Add Score from imported Custom Field
    			if ( 1 < strlen($post->post_score) ) {
    			 	// Score exists.
    				add_post_meta($post_id, 'SCORE', $post->post_score);
    			}

    I also added this bit further down in the list of "else ifs"...

    } else if ( 'SCORE:' == $line ) {
    				$context = 'score';

    AND.. this bit further down in the // Processing multi-line field, check context section..

    } else if ( 'score' == $context ) {
    					$post->post_score .= $line;

    Now to tackle TAGS:

  19. jayseae
    Member
    Posted 3 years ago #

    Sounds like you are making good progress. Tags should be fairly similar - you're just working with a differently named field here. The importer does not do anything with them (I don't think), so you should be just about there!

  20. Joe Siegler
    Member
    Posted 3 years ago #

    That's proving a bit harder for some reason.

    } else if ( 0 === strpos($line, 'TAGS:') ) {
    				$gamedate = trim( substr($line, strlen('TAGS:')) );
    				echo $gamedate;

    Using that code shows me it's picking up the value from TAGS:, as I stuck in the echo line to check that out.

    But whenever I try to import it, nothing happens. Can't seem to get the right bit for add_post_meta.

  21. Joe Siegler
    Member
    Posted 3 years ago #

    Got TAGS.

    Added this bit in the // add tags or keywords section..

    // Add Gamedate from imported tags field
     			if ( 1 < strlen($post->post_tags) ) {
    			 	// Gamedate exists.
    				add_post_meta($post_id, 'GAMEDATE', $post->post_tags);
    
    			}

    Then added this bit down by the list if else if's..

    } else if ( 0 === strpos($line, 'TAGS:') ) {
    				$tags = trim( substr($line, strlen('TAGS:')) );
    				if ( '' == $context )
    					$post->post_tags = $tags;

    That lets me get everything in there the way I want. There's conditionals in there, as these three bits are only there if it's an actual game recap. If it's something else (general blog post, book review, etc), they won't be there, so I checked one of those on my test import, and it worked on those as well by NOT creating the custom fields.

    Now comes the addtl fun - creating the templates that use these entries. :)

    Thanks jayseae for letting me think out loud here in these, and for the general direction you led me in. :)

Topic Closed

This topic has been closed to new replies.

About this Topic