No matching German post for slug: {$slug}
"; continue; } if (function_exists('pll_get_post_language') && pll_get_post_language($german_post->ID) !== 'de') { echo "German post {$slug} is not set to 'de' language in Polylang.
"; continue; } // Check existing Polylang translation $existing_translations = function_exists('pll_get_post_translations') ? pll_get_post_translations($german_post->ID) : []; if (isset($existing_translations['en'])) { echo "Already linked to English: {$slug}
"; continue; } // Try to find an existing English post with the same slug $maybe_existing_en_post = get_page_by_path($slug, OBJECT, 'post'); $english_post_id = 0; if ($maybe_existing_en_post && pll_get_post_language($maybe_existing_en_post->ID) === 'en') { $english_post_id = $maybe_existing_en_post->ID; // Link them via Polylang if (function_exists('pll_save_post_translations')) { pll_save_post_translations([ 'de' => $german_post->ID, 'en' => $english_post_id, ]); } echo "Linked existing English post with German: {$slug}
"; continue; } $title = (string)$item->title; $content = (string)$item->children('content', true)->encoded; $excerpt = (string)$item->children('excerpt', true)->encoded; $status = (string)$item->children('wp', true)->status; $date = (string)$item->children('wp', true)->post_date; $modified = (string)$item->children('wp', true)->post_modified; $comment_status = (string)$item->children('wp', true)->comment_status; $ping_status = (string)$item->children('wp', true)->ping_status; $menu_order = (int)$item->children('wp', true)->menu_order; $password = (string)$item->children('wp', true)->post_password; $is_sticky = (int)$item->children('wp', true)->is_sticky; $author = $german_post->post_author; $post_data = [ 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_status' => $status, 'post_type' => 'post', 'post_name' => $slug, 'post_author' => $author, 'post_date' => $date, 'post_date_gmt' => get_gmt_from_date($date), 'post_modified' => $modified, 'post_modified_gmt' => get_gmt_from_date($modified), 'comment_status' => $comment_status, 'ping_status' => $ping_status, 'menu_order' => $menu_order, 'post_password' => $password, ]; $english_post_id = wp_insert_post($post_data); if (is_wp_error($english_post_id)) { echo "Error creating English post for slug: {$slug}
"; continue; } if ($is_sticky) { stick_post($english_post_id); } // Categories & Tags from German, linked or created for English pll_set_post_language($english_post_id, 'en'); if (function_exists('pll_get_term')) { $taxonomies = ['category', 'post_tag']; foreach ($taxonomies as $taxonomy) { $german_terms = wp_get_object_terms($german_post->ID, $taxonomy, ['fields' => 'all']); if (!empty($german_terms) && !is_wp_error($german_terms)) { $english_term_ids = []; foreach ($german_terms as $german_term) { $german_term_id = $german_term->term_id; // Get existing English equivalent $english_term_id = pll_get_term($german_term_id, 'en'); if ($english_term_id) { $english_term_ids[] = (int) $english_term_id; } } if (!empty($english_term_ids)) { wp_set_object_terms($english_post_id, $english_term_ids, $taxonomy); } } } } // Link Polylang translation if (function_exists('pll_set_post_language') && function_exists('pll_save_post_translations')) { pll_save_post_translations([ 'de' => $german_post->ID, 'en' => $english_post_id, ]); } echo "✅ Imported and linked: {$slug}
"; } }