'English', 2 => 'Galego', 3 => 'Español', 4 => 'Português', 5 => 'Català' ); $created = 0; $assigned = 0; foreach ($idiomas_map as $id => $nombre) { // Crear o encontrar tag $term = get_term_by('name', $nombre, 'post_tag'); if (!$term) { $result = wp_insert_term($nombre, 'post_tag', array( 'slug' => sanitize_title($nombre) )); if (is_wp_error($result)) continue; $term_id = $result['term_id']; $created++; } else { $term_id = $term->term_id; } // Buscar posts $posts = $wpdb->get_col($wpdb->prepare(" SELECT DISTINCT pm.post_id FROM wp_postmeta pm JOIN wp_posts p ON pm.post_id = p.ID WHERE pm.meta_key = 'Idioma' AND pm.meta_value = %s AND p.post_type = 'post' AND p.post_status = 'publish' ", $id)); foreach ($posts as $post_id) { wp_set_object_terms($post_id, $term_id, 'post_tag', true); $assigned++; } echo "$nombre: " . count($posts) . " posts\n"; } echo "\nTags creados: $created\n"; echo "Asignaciones: $assigned\n"; echo "=== FIN ===\n";