Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am migrating non-wp database to Wordpress (for the first time in my life, and hopefully the last).

As both new and old websites are multilingual, one of the tasks for me is to connect the articles with its translations. I am using WPML plugin for multi language support on Wordpress. This plugin creates a table, wp_icl_translations with the following structure:

element_id | trid       | language_code           | source_language_code
---------------------------------------------------------------------------
(post id)  | (group id) | (of the translated post)| (of the original post)

trid is the id of the original post id and all translations of one original post have the same value for the trid. If the post is original, trid is the same as element_id which is the post id, and source_language_code is NULL.

Couple of notes on the posts I have:

  • Not all posts are translated to all the languages (I have 8 languages)
  • There are posts that have only original entries in any language

In the old database posts had item_id which was the same for all the translations of one article. When I migrated them, I added this item_id as meta_value for each posts. I did the same for the language code, so I was able to create a custom table where I would have a connection between post ids of the wordpress database and item_ids and language codes of old database. This custom table, id_item_lid, has the following structure:

id                           | item_id                     | lid
---------------------------------------------------------------------------
post id assigned by wordress | old item_id                 | language code
                             | (translations share this id)|

So in short I need to update trid column of wp_icl_translations with the post ids of posts that share the same item_id in id_item_lid table.

Here is the function I came up with:

function rkm_translation_update() {
global $wpdb;
$trans_row = $wpdb->get_row("SELECT * FROM id_item_lid", OBJECT, 0);
$id = $trans_row->id;
$item = $trans_row->item_id;
$lid = $trans_row->lid;

$trans_array = $wpdb->get_results("SELECT * FROM id_item_lid");
foreach ($trans_array as $trans) {
    $id_new = $trans->id;
    $item_new = $trans->item_id;
    $lid_new = $trans->lid;
    if ($item === $item_new) {
        $wpdb->update('wp_icl_translations', array('trid' => $id, 'source_language_code' => $lid), array('element_id' => $id_new));
    } else {
        $id = $trans->id;
        $item = $trans->item_id;
        $lid = $trans->lid;
    }
}
}

add_action( 'init', 'rkm_translation_update');

Idea behind it is to take the records of the first row, and compare it to the next. But, nothing happens. I am doing this kind of database manipulation for the first time, so I don't know where should I start with the debugging.

Any help and direction is highly appreciated.

If you need more info please do not hesitate to ask.

Thanks!

share|improve this question

1 Answer 1

Logic of my function was all wrong, should have sorted the result in $trans_array before the foreach loop.

Here is the updated code that worked, if anyone needs it:

function rkm_translation_update() {
    global $wpdb;
    $trans_row = $wpdb->get_row("SELECT * FROM id_item_lid", OBJECT, 0);
    $id = $trans_row->id;
    $item = $trans_row->item_id;
    $lid = $trans_row->lid;

    $trans_array = $wpdb->get_results("SELECT * FROM id_item_lid ORDER BY item_id");
    foreach ($trans_array as $trans) {
        $id_new = $trans->id;
        $item_new = $trans->item_id;
        $lid_new = $trans->lid;
        if ($item === $item_new) {
            $wpdb->update('wp_icl_translations', array('trid' => $id, 'source_language_code' => $lid), array('element_id' => $id_new, 'element_type' => 'post_post'));
        } else {
             $id = $trans->id;
             $item = $trans->item_id;
             $lid = $trans->lid;
             $wpdb->update('wp_icl_translations', array('trid' => $id, 'source_language_code' => $lid), array('element_id' => $id, 'element_type' => 'post_post'));
        }
    }
}

This is a bit of a dirty solution, when debugging is true, there are some warnings in the admin related to original post language, if it is missing, probably could be refined a bit more, but I couldn't be bothered.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.