Files
feadulta/wordpress/migrate_metadata.php
T
rafa 668d973889 Initial commit — migración Joomla→WordPress feadulta.org
Incluye: mu-plugins custom (fea-homepage, carta-semana), scripts de
migración/traducción/deploy, documentación de auditoría, análisis de
cartas y HTML de evangelios exportados desde Joomla.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 08:33:12 -04:00

211 lines
6.2 KiB
PHP

<?php
/**
* Script para migrar metadatos de Joomla a categories/tags de WordPress
* Ejecutar: wp eval-file migrate_metadata.php
*/
// Cargar WordPress
require_once('/var/www/html/wp-load.php');
echo "=== Iniciando migracion de metadatos ===\n\n";
// 1. Migrar AUTOR -> category
echo "1. Procesando AUTORES...\n";
$autores = $wpdb->get_results("
SELECT DISTINCT meta_value as autor, COUNT(*) as cnt
FROM wp_postmeta
WHERE meta_key = 'Autor' AND meta_value IS NOT NULL AND meta_value != ''
ORDER BY cnt DESC
");
$created = 0;
$assigned = 0;
foreach ($autores as $autor) {
if (empty($autor->autor)) continue;
// Buscar si ya existe la categoria
$term = get_term_by('name', $autor->autor, 'category');
if (!$term) {
$result = wp_insert_term($autor->autor, 'category', array(
'description' => 'Autor migrado desde Joomla: ' . $autor->autor,
'slug' => sanitize_title($autor->autor)
));
if (is_wp_error($result)) {
echo " - Error creando categoria '$autor->autor': " . $result->get_error_message() . "\n";
continue;
}
$term_id = $result['term_id'];
$created++;
} else {
$term_id = $term->term_id;
}
// Asignar a todos los posts que tienen este autor
$posts = $wpdb->get_results($wpdb->prepare("
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'Autor' AND meta_value = %s
", $autor->autor));
foreach ($posts as $post) {
wp_set_object_terms($post->post_id, $term_id, 'category', true);
$assigned++;
}
echo " - $autor->autor: $autor->cnt posts -> categoria ID $term_id\n";
}
echo " Categorias de autor creadas: $created\n";
echo " Asignaciones realizadas: $assigned\n\n";
// 2. Migrar IDIOMA -> post_tag
echo "2. Procesando IDIOMAS...\n";
$idiomas = $wpdb->get_results("
SELECT DISTINCT meta_value as idioma, COUNT(*) as cnt
FROM wp_postmeta
WHERE meta_key = 'Idioma' AND meta_value IS NOT NULL AND meta_value != ''
ORDER BY cnt DESC
");
$created_tags = 0;
$assigned_tags = 0;
foreach ($idiomas as $idioma) {
if (empty($idioma->idioma)) continue;
$term = get_term_by('name', $idioma->idioma, 'post_tag');
if (!$term) {
$result = wp_insert_term($idioma->idioma, 'post_tag', array(
'description' => 'Idioma migrado desde Joomla',
'slug' => sanitize_title($idioma->idioma)
));
if (is_wp_error($result)) {
echo " - Error creando tag '$idioma->idioma': " . $result->get_error_message() . "\n";
continue;
}
$term_id = $result['term_id'];
$created_tags++;
} else {
$term_id = $term->term_id;
}
$posts = $wpdb->get_results($wpdb->prepare("
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'Idioma' AND meta_value = %s
", $idioma->idioma));
foreach ($posts as $post) {
wp_set_object_terms($post->post_id, $term_id, 'post_tag', true);
$assigned_tags++;
}
echo " - $idioma->idioma: $idioma->cnt posts\n";
}
echo " Tags de idioma creados: $created_tags\n";
echo " Asignaciones: $assigned_tags\n\n";
// 3. Migrar TEMA -> category (subcategoria de TEMAS)
echo "3. Procesando TEMAS...\n";
// Crear categoria padre TEMAS si no existe
$temas_parent = get_term_by('name', 'TEMAS', 'category');
if (!$temas_parent) {
$result = wp_insert_term('TEMAS', 'category', array(
'description' => 'Categoria padre para temas',
'slug' => 'temas'
));
$temas_parent = get_term_by('name', 'TEMAS', 'category');
}
$temas = $wpdb->get_results("
SELECT DISTINCT meta_value as tema, COUNT(*) as cnt
FROM wp_postmeta
WHERE meta_key = 'Tema' AND meta_value IS NOT NULL AND meta_value != ''
ORDER BY cnt DESC
LIMIT 50
");
$created_temas = 0;
foreach ($temas as $tema) {
if (empty($tema->tema)) continue;
$term = get_term_by('name', $tema->tema, 'category');
if (!$term) {
$result = wp_insert_term($tema->tema, 'category', array(
'description' => 'Tema migrado desde Joomla',
'slug' => sanitize_title($tema->tema),
'parent' => $temas_parent->term_id
));
if (!is_wp_error($result)) {
$created_temas++;
}
}
// Asignar
$posts = $wpdb->get_results($wpdb->prepare("
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'Tema' AND meta_value = %s
", $tema->tema));
$term = get_term_by('name', $tema->tema, 'category');
if ($term) {
foreach ($posts as $post) {
wp_set_object_terms($post->post_id, $term->term_id, 'category', true);
}
}
echo " - $tema->tema: $tema->cnt posts\n";
}
echo " Categorias de tema creadas: $created_temas\n\n";
// 4. Migrar LIBRO DE LA BIBLIA -> category
echo "4. Procesando LIBROS DE LA BIBLIA...\n";
$libros = $wpdb->get_results("
SELECT DISTINCT meta_value as libro, COUNT(*) as cnt
FROM wp_postmeta
WHERE meta_key = 'Libro de la biblia' AND meta_value IS NOT NULL AND meta_value != ''
ORDER BY cnt DESC
LIMIT 100
");
$created_libros = 0;
foreach ($libros as $libro) {
if (empty($libro->libro)) continue;
$term = get_term_by('name', $libro->libro, 'category');
if (!$term) {
$result = wp_insert_term($libro->libro, 'category', array(
'description' => 'Libro biblico migrado desde Joomla',
'slug' => sanitize_title($libro->libro)
));
if (!is_wp_error($result)) {
$created_libros++;
}
}
$posts = $wpdb->get_results($wpdb->prepare("
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'Libro de la biblia' AND meta_value = %s
", $libro->libro));
$term = get_term_by('name', $libro->libro, 'category');
if ($term) {
foreach ($posts as $post) {
wp_set_object_terms($post->post_id, $term->term_id, 'category', true);
}
}
echo " - $libro->libro: $libro->cnt posts\n";
}
echo " Categorias de libros biblicos creadas: $created_libros\n\n";
echo "=== Migracion completada ===\n";